Hi Guys,
How to get First day of previous month and last day of previous month from trunc(sysdate) using SQL..?
Thanks in advance.
Br,
pinpe
Hi Guys,
How to get First day of previous month and last day of previous month from trunc(sysdate) using SQL..?
Thanks in advance.
Br,
pinpe
SQL> select sysdate from dual;
SYSDATE
---------
06-AUG-11
SQL> select trunc(trunc(sysdate,'MM')-1,'MM') "First Day of Last Month" from dual;
First Day
---------
01-JUL-11
SQL> select trunc(sysdate,'MM')-1 "Last Day of Last Month" from dual;
Last Day
---------
31-JUL-11
SQL>
test@ORA11G>
test@ORA11G>
test@ORA11G> select sysdate as today,
2 trunc(add_months(sysdate,-1),'mm') as prev_mnth_first_day,
3 last_day(add_months(sysdate,-1)) as prev_mnth_last_day
4 from dual
5 /
TODAY PREV_MNTH_FIRST_DAY PREV_MNTH_LAST_DAY
--------- -------------------- --------------------
21-AUG-11 01-JUL-11 31-JUL-11
test@ORA11G>
test@ORA11G>
Here:
(a) add_months (<date>, -1) returns the same day (as today), but for previous month
(b) Trunc (<date>, 'mm') returns 1st day of the month in which <date> lies
(c) Last_Day (<date>) returns the last day of the month in which <date> lies
tyler_durden