Protected by Copyscape Web Copyright Protection Software

Pages

Search This Blog

Monday, April 28, 2014

Date Related Query in Oracle SQL

Hi All,

I need one query to display a date of 01-May-year.
I am giving input as date. So requirement is when I gave one date I need 01-May-Past Year.

If I give 20-Jan-2012 I need to display as 01-May-2011
If I give 20-May-2012 I need to display 01-May-2012
If I give 20-Mar-2012 I need to display as 01-May-2011

Answer:
------------

 SELECT
dd,
add_months(trunc(dd,'yyyy'),CASE WHEN EXTRACT(MONTH FROM dd) < 5 THEN -8 ELSE 4 END) nd
FROM (SELECT to_date (:P_DATE,'DD-MON-YYYY') dd FROM dual );



Here is the another answer:


SELECT ADD_MONTHS( TRUNC( ADD_MONTHS( TO_DATE (:P_DATE,'DD-MON-YYYY'), -4), 'Y'), 4)
FROM dual;


*****************************Keep Smiling*******************************************

No comments:

Post a Comment