Connecting Tech Pros Worldwide Forums | Help | Site Map

DATE FUNCTIONS

amitpatel66's Avatar
Moderator
 
Join Date: Mar 2007
Location: Hyderabad, India
Posts: 2,192
#1   Sep 17 '07
DATE Functions:

MONTHS_BETWEEN(date1, date2) - Returns number of months between date1 and date2
Expand|Select|Wrap|Line Numbers
  1. SELECT MONTHS_BETWEEN('01-JAN-2007', SYSDATE) FROM dual -- returns -8.43767398446834 (Here SYSDATE is 14-SEP-2007)
  2.  
ADD_MONTHS(date1,m) - Returns date with m number of months added to date1
Expand|Select|Wrap|Line Numbers
  1. SELECT ADD_MONTHS('08-JAN-2007',2) FROM dual -- returns '08-MAR-2007'
  2.  
LAST_DAY(date1) - Returns last day(date) of the month of date1
Expand|Select|Wrap|Line Numbers
  1. SELECT LAST_DAY('08-SEP-2007') FROM dual -- returns '30-SEP-2007'
  2.  
NEXT_DAY(date1,m) - Returns mth day from date1
Expand|Select|Wrap|Line Numbers
  1. SELECT NEXT_DAY('08-SEP-2007',1) FROM dual -- returns '09-SEP-2007'
  2.  
In the above example, '08-SEP-2007' is Saturday and number 1 represents the day of the week ie Sunday here. So the above query returns Sunday's date after '08-SEP-2007' ie '09-SEP-2007'

Days of the Week that can be used in NEXT_DAY function:

1 - Sunday
2 - Monday
3 - Tuesday
4 - Wednesday
5 - Thrusday
6 - Friday
7 - Saturday

ROUND(date1) - Returns Next day (date) of date1 if the time elapsed for the date1 is more than 12 hours.
Expand|Select|Wrap|Line Numbers
  1. SELECT ROUND(SYSDATE) FROM dual -- returns '15-SEP-2007' (Here SYSDATE is '9/14/2007 2:00:27 PM')
  2.  
TRUNC(date1) - Returns Current day of date1 after truncating TIME value from date1.
Expand|Select|Wrap|Line Numbers
  1. SELECT TRUNC(SYSDATE) FROM dual -- returns '14-SEP-2007' (Here SYSDATE is '9/14/2007 2:00:27 PM')
  2.  



Closed Thread