Useful Date and Time Functions in PL/SQL

11 Feb, 2019
Date and Time Function formats are different various database. we are going to discuss most common functions used in Oracle database.
The function SYSDATE returns 7 bytes of data, which includes:
Century
Year
Month
Day
Hour
Minute
Second
1. Extract():Oracle helps you to extract Year, Month and Day from a date using Extract() Function.
Example-1: Extracting Year:SELECT SYSDATE AS CURRENT_DATE_TIME, EXTRACT(Year FROM SYSDATE) AS ONLY_CURRENT_YEARFROM DualOutput:CURRENT_DATE_TIMEONLY_CURRENT_YEAR05.Feb.2019 07:29:242019Explanation:Useful to retrieve only year from the System date/Current date or particular specified date.
SELECT SYSDATE AS CURRENT_DATE_TIME, EXTRACT(Year FROM SYSDATE) AS ONLY_CURRENT_YEARFROM Dual
Output:
Explanation:Useful to retrieve only year from the System date/Current date or particular specified date.
Example-2: Extracting Month:SELECT SYSDATE AS CURRENT_DATE_TIME, EXTRACT(Month FROM SYSDATE) AS ONLY_CURRENT_MONTHFROM DualOutput:CURRENT_DATE_TIMEONLY_CURRENT_MONTH05.Feb.2019 07:29:24FebExplanation:Useful to retrieve only month from the System date/Current date or particular specified date.
SELECT SYSDATE AS CURRENT_DATE_TIME, EXTRACT(Month FROM SYSDATE) AS ONLY_CURRENT_MONTHFROM Dual
Output:
Explanation:Useful to retrieve only month from the System date/Current date or particular specified date.
Example-3: Extracting Day:SELECT SYSDATE AS CURRENT_DATE_TIME, EXTRACT(Day FROM SYSDATE) AS ONLY_CURRENT_DAYFROM DualOutput:CURRENT_DATE_TIMEONLY_CURRENT_DAY05.Feb.2019 07:29:245Explanation:Useful to retrieve only day from the System date/Current date or particular specified date.
SELECT SYSDATE AS CURRENT_DATE_TIME, EXTRACT(Day FROM SYSDATE) AS ONLY_CURRENT_DAYFROM Dual
Output:
Explanation:Useful to retrieve only day from the System date/Current date or particular specified date.
2. ADD_MONTHS (date, n):Using this method in PL/SQL you can add as well as subtract number of months(n) to a date. Here ‘n’ can be both negative or positive.
Example-4:SELECT ADD_MONTHS(SYSDATE, -1)  AS PREV_MONTH, SYSDATE AS CURRENT_DATE,                                  ADD_MONTHS(SYSDATE, 1) as NEXT_MONTHFROM DualOutput:PREV_MONTHCURRENT_DATENEXT_MONTH02.Jan.2019 09:15:4602.Feb.2019 09:15:4602.Mar.2019 09:15:46Explanation:ADD_MONTHS function have two parameters one is date, where it could be any specified/particular date or System date as current date and second is ‘n’, it is an integer value could be positive or negative to get upcoming date or previous date.
SELECT ADD_MONTHS(SYSDATE, -1)  AS PREV_MONTH, SYSDATE AS CURRENT_DATE,                                  ADD_MONTHS(SYSDATE, 1) as NEXT_MONTHFROM Dual
Output:
Explanation:ADD_MONTHS function have two parameters one is date, where it could be any specified/particular date or System date as current date and second is ‘n’, it is an integer value could be positive or negative to get upcoming date or previous date.
3. LAST_DAY(date):Using this method in PL/SQL you can get the last day in the month of specified date.
Example-5:SELECT SYSDATE AS CURRENT_DATE, LAST_DAY(SYSDATE) AS LAST_DAY_OF_MONTH,                                 LAST_DAY(SYSDATE)+1 AS FIRST_DAY_OF_NEXT_MONTHFROM DualOutput:CURRENT_DATELAST_DAY_OF_MONTHFIRST_DAY_OF_NEXT_MONTH02.Feb.2019 09:32:0028.Feb.2019 09:32:0001.Mar.2019 09:32:00Explanation:In above example, we are getting current date using SYSDATE function and last date of the month would be retrieved using LAST_DAY function and this function be also helpful for retrieving the first day of the next month.
SELECT SYSDATE AS CURRENT_DATE, LAST_DAY(SYSDATE) AS LAST_DAY_OF_MONTH,                                 LAST_DAY(SYSDATE)+1 AS FIRST_DAY_OF_NEXT_MONTHFROM Dual
Output:
Explanation:In above example, we are getting current date using SYSDATE function and last date of the month would be retrieved using LAST_DAY function and this function be also helpful for retrieving the first day of the next month.
Example-6: Number of Days left in the monthSELECT SYSDATE AS CURRENT_DATE, LAST_DAY(SYSDATE) - SYSDATE AS DAYS_LEFT_IN_MONTHFROM DualOutput:CURRENT_DATEDAYS_LEFT_IN_MONTH02.Feb.2019 09:32:0026
SELECT SYSDATE AS CURRENT_DATE, LAST_DAY(SYSDATE) - SYSDATE AS DAYS_LEFT_IN_MONTHFROM Dual
Output:
4. MONTHS_BETWEEN (date1, date2):Using this method in PL/SQL you can calculate the number of months between two entered dates date1 and date2. if date1 is later than date2 then the result would be positive and if date1 is earlier than date2 then result is negative.
Note:If a fractional month is calculated, the MONTHS_BETWEEN function calculates the fraction based on a 31-day month.
Example-7:SELECT MONTHS_BETWEEN (TO_DATE ('01-07-2003', 'dd-mm-yyyy'),                        TO_DATE ('14-03-2003', 'dd-mm-yyyy')) AS NUMBER_OF_MONTHSFROM DualOutput:NUMBER_OF_MONTHS3.58Explanation:Here date1 and date2 are not on the same day of the month that’s why we are getting the value in fractions, as well as date1 is later than date2 so the resulting value is in integers.Eneterd date should be in particular date format, that is the reason of using TO_DATE function while comparison within MONTHS_BETWEEN function.Let’s select the number of months an employee has worked for the company.
SELECT MONTHS_BETWEEN (TO_DATE ('01-07-2003', 'dd-mm-yyyy'),                        TO_DATE ('14-03-2003', 'dd-mm-yyyy')) AS NUMBER_OF_MONTHSFROM Dual
Output:
Explanation:Here date1 and date2 are not on the same day of the month that’s why we are getting the value in fractions, as well as date1 is later than date2 so the resulting value is in integers.Eneterd date should be in particular date format, that is the reason of using TO_DATE function while comparison within MONTHS_BETWEEN function.
Let’s select the number of months an employee has worked for the company.
Example-8:SELECT MONTHS_BETWEEN (SYSDATE, DATE_OF_HIRE) AS NUMBER_OF_MONTHSFROM EmployeesInput:SYSTEM_DATEDATE_OF_HIRE02-02-201931-10-201702-02-201903-12-201702-02-201924-09-201802-02-201922-12-201602-02-201918-06-2018Output:NUMBER_OF_MONTHS15.06413.9674.29025.3547.483
SELECT MONTHS_BETWEEN (SYSDATE, DATE_OF_HIRE) AS NUMBER_OF_MONTHSFROM Employees
Input:
Output:
5. NEXT_DAY(date, day_of_week):It will return the upcoming date of the first weekday that is later than the entered date.It has two parameters first date where, system date or specified date can be entered; second day of week which should be in character form.
Example-9:SELECT NEXT_DAY(SYSDATE, 'SUNDAY') AS NEXT_SUNDAYFROM DualOutput:NEXT_SUNDAY17-FEB-2019Explanation:It will help to provide the next upcoming date corresponding to the day, return type is always DATE regardless of datatype date. The second parameter must be a day of the week either full name or abbreviated.
SELECT NEXT_DAY(SYSDATE, 'SUNDAY') AS NEXT_SUNDAYFROM Dual
Output:
Explanation:It will help to provide the next upcoming date corresponding to the day, return type is always DATE regardless of datatype date. The second parameter must be a day of the week either full name or abbreviated.
SQL-PL/SQL
SQL
SQL
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.