Date manipulating functions in SQL - GeeksforGeeks

21 Oct, 2020
To manipulate and fetch date values from the table, oracle has inbuilt date functions. Here, we are going to coverDate manipulating functions in SQL.
ADD_MONTHS :It will add the total number of months specified in n to the date parameter and Returns date.Syntax :ADD_MONTHS(d, n)Example –Select ADD_MONTHS(SYSDATE, 4) 
"Add months" from dual;
Output :Add Months
01-NOV-04
LAST_DAY :In the LAST_DAY function, It will return the last date of the month, and for the specified month like if the system date is 1 Nov then it will return 30 Nov.Syntax :LAST_DAY(date)Example-Select SYSDATE, LAST_DAY(SYSDATE) 
"Last Day" from dual;
Output :SYSDATE                Last Day
01-NOV-04             30-NOV-04
MONTHS_BETWEEN :Returns number of months between date1 and date2.Syntax :MONTHS_BETWEEN(date1, date2)Example –Select MONTHS_BETWEEN('02-FEB-00', '01-JAN-00') 
"Months" from dual;
Output :Months
  1
NEXT_DAY :It will return the date of the first weekday that comes after the date specified in date parameter. char specified should be someday of the week.Syntax :NEXT_DAY(date, char)Example –SELECT NEXT_DAY('06-JUL-02', 'saturday') 
"Next day" from dual;
here it returns the date of next saturday.Output :Next day
13-JUL-02
NEW_TIME :Returns the date after converting it from time zone 1 to a date in time zone 2.Syntax :NEW_TIME(date, zone1, zone2)ValueDescriptionValueDescriptionASTAtlantic Standard TimeADTAtlantic Daylight TimeBSTBering Standard TimeBDTBering daylight TimeCSTCentral Standard TimeCDTCentral daylightTimeGMTGreenwich Mean TimeNSTNewfoundland standard timeHDTAlaska -Hawaii daylight TimeHSTAlaska -Hawaii standard TimeMDTMountain daylight TimeMSTMountain standard TimePSTpacific standard TimePDTpacific daylight TimeExample –Select NEW_TIME(To_date('2004/07/01 01:45', 'yyyy/mm/dd HH24:MI'), 
'AST', 'MST') "MST" from dual;
Output :MST
30-JUN-04
This converts an Atlantic standard time into mountain standard time.
ADD_MONTHS :It will add the total number of months specified in n to the date parameter and Returns date.Syntax :ADD_MONTHS(d, n)Example –Select ADD_MONTHS(SYSDATE, 4) 
"Add months" from dual;
Output :Add Months
01-NOV-04

Syntax :
ADD_MONTHS(d, n)
Example –
Select ADD_MONTHS(SYSDATE, 4) 
"Add months" from dual;

Output :
Add Months
01-NOV-04

LAST_DAY :In the LAST_DAY function, It will return the last date of the month, and for the specified month like if the system date is 1 Nov then it will return 30 Nov.Syntax :LAST_DAY(date)Example-Select SYSDATE, LAST_DAY(SYSDATE) 
"Last Day" from dual;
Output :SYSDATE                Last Day
01-NOV-04             30-NOV-04

Syntax :
LAST_DAY(date)
Example-
Select SYSDATE, LAST_DAY(SYSDATE) 
"Last Day" from dual;

Output :
SYSDATE                Last Day
01-NOV-04             30-NOV-04

MONTHS_BETWEEN :Returns number of months between date1 and date2.Syntax :MONTHS_BETWEEN(date1, date2)Example –Select MONTHS_BETWEEN('02-FEB-00', '01-JAN-00') 
"Months" from dual;
Output :Months
  1

Syntax :
MONTHS_BETWEEN(date1, date2)
Example –
Select MONTHS_BETWEEN('02-FEB-00', '01-JAN-00') 
"Months" from dual;

Output :
Months
  1

NEXT_DAY :It will return the date of the first weekday that comes after the date specified in date parameter. char specified should be someday of the week.Syntax :NEXT_DAY(date, char)Example –SELECT NEXT_DAY('06-JUL-02', 'saturday') 
"Next day" from dual;
here it returns the date of next saturday.Output :Next day
13-JUL-02

Syntax :
NEXT_DAY(date, char)
Example –
SELECT NEXT_DAY('06-JUL-02', 'saturday') 
"Next day" from dual;

here it returns the date of next saturday.Output :
Next day
13-JUL-02

NEW_TIME :Returns the date after converting it from time zone 1 to a date in time zone 2.Syntax :NEW_TIME(date, zone1, zone2)ValueDescriptionValueDescriptionASTAtlantic Standard TimeADTAtlantic Daylight TimeBSTBering Standard TimeBDTBering daylight TimeCSTCentral Standard TimeCDTCentral daylightTimeGMTGreenwich Mean TimeNSTNewfoundland standard timeHDTAlaska -Hawaii daylight TimeHSTAlaska -Hawaii standard TimeMDTMountain daylight TimeMSTMountain standard TimePSTpacific standard TimePDTpacific daylight TimeExample –Select NEW_TIME(To_date('2004/07/01 01:45', 'yyyy/mm/dd HH24:MI'), 
'AST', 'MST') "MST" from dual;
Output :MST
30-JUN-04
This converts an Atlantic standard time into mountain standard time.
Syntax :
NEW_TIME(date, zone1, zone2)
Example –
Select NEW_TIME(To_date('2004/07/01 01:45', 'yyyy/mm/dd HH24:MI'), 
'AST', 'MST') "MST" from dual;

Output :
MST
30-JUN-04

This converts an Atlantic standard time into mountain standard time.
DBMS-SQL
SQL
SQL
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Update Multiple Columns in Single Update Statement in SQL?
SQL | Subquery
How to Create a Table With Multiple Foreign Keys in SQL?
What is Temporary Table in SQL?
SQL Query to Find the Name of a Person Whose Name Starts with Specific Letter
SQL using Python
SQL Query to Convert VARCHAR to INT
How to Write a SQL Query For a Specific Date Range and Date Time?
How to Select Data Between Two Dates and Times in SQL Server?
SQL Query to Compare Two Dates