Open In App

Useful Date and Time Functions in PL/SQL

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:

1. Extract(): Oracle helps you to extract Year, Month and Day from a date using Extract() Function.






SELECT SYSDATE AS CURRENT_DATE_TIME, EXTRACT(Year FROM SYSDATE) AS ONLY_CURRENT_YEAR
FROM Dual

CURRENT_DATE_TIME ONLY_CURRENT_YEAR
05.Feb.2019 07:29:24 2019




SELECT SYSDATE AS CURRENT_DATE_TIME, EXTRACT(Month FROM SYSDATE) AS ONLY_CURRENT_MONTH
FROM Dual

CURRENT_DATE_TIME ONLY_CURRENT_MONTH
05.Feb.2019 07:29:24 Feb




SELECT SYSDATE AS CURRENT_DATE_TIME, EXTRACT(Day FROM SYSDATE) AS ONLY_CURRENT_DAY
FROM Dual

CURRENT_DATE_TIME ONLY_CURRENT_DAY
05.Feb.2019 07:29:24 5

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.






SELECT ADD_MONTHS(SYSDATE, -1)  AS PREV_MONTH, SYSDATE AS CURRENT_DATE,
                                 ADD_MONTHS(SYSDATE, 1) as NEXT_MONTH
FROM Dual

PREV_MONTH CURRENT_DATE NEXT_MONTH
02.Jan.2019 09:15:46 02.Feb.2019 09:15:46 02.Mar.2019 09:15:46

3. LAST_DAY(date): Using this method in PL/SQL you can get the last day in the month of specified date.




SELECT SYSDATE AS CURRENT_DATE, LAST_DAY(SYSDATE) AS LAST_DAY_OF_MONTH,
                                LAST_DAY(SYSDATE)+1 AS FIRST_DAY_OF_NEXT_MONTH
FROM Dual

CURRENT_DATE LAST_DAY_OF_MONTH FIRST_DAY_OF_NEXT_MONTH
02.Feb.2019 09:32:00 28.Feb.2019 09:32:00 01.Mar.2019 09:32:00




SELECT SYSDATE AS CURRENT_DATE, LAST_DAY(SYSDATE) - SYSDATE AS DAYS_LEFT_IN_MONTH
FROM Dual

CURRENT_DATE DAYS_LEFT_IN_MONTH
02.Feb.2019 09:32:00 26

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.




SELECT MONTHS_BETWEEN (TO_DATE ('01-07-2003', 'dd-mm-yyyy'),
                       TO_DATE ('14-03-2003', 'dd-mm-yyyy')) AS NUMBER_OF_MONTHS
FROM Dual

NUMBER_OF_MONTHS
3.58




SELECT MONTHS_BETWEEN (SYSDATE, DATE_OF_HIRE) AS NUMBER_OF_MONTHS
FROM Employees

SYSTEM_DATE DATE_OF_HIRE
02-02-2019 31-10-2017
02-02-2019 03-12-2017
02-02-2019 24-09-2018
02-02-2019 22-12-2016
02-02-2019 18-06-2018
NUMBER_OF_MONTHS
15.064
13.967
4.290
25.354
7.483

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.




SELECT NEXT_DAY(SYSDATE, 'SUNDAY') AS NEXT_SUNDAY
FROM Dual

NEXT_SUNDAY
17-FEB-2019

Article Tags :
SQL