0% found this document useful (0 votes)
65 views2 pages

Date Function

This document provides SQL queries to find dates for various date/time periods including the current, next, and previous week, month, quarter, and year. It includes queries to find the first and last day of each period as well as determining the number of days between dates and remaining or passed in the current time period. Examples are provided for finding the start and end dates of each month in a year.

Uploaded by

NIZAR MOHAMMED
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
65 views2 pages

Date Function

This document provides SQL queries to find dates for various date/time periods including the current, next, and previous week, month, quarter, and year. It includes queries to find the first and last day of each period as well as determining the number of days between dates and remaining or passed in the current time period. Examples are provided for finding the start and end dates of each month in a year.

Uploaded by

NIZAR MOHAMMED
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

Date & Time Function

1. How to find the First day of Current Week, i.e. Sunday?


SELECT TRUNC(SYSDATE,'DAY')FROM DUAL;

2. How to find the First day of Next Week, i.e. Sunday?


SELECT TRUNC(SYSDATE+7,'DAY')FROM DUAL;

3. How to find the First day of Previous Week, i.e. Sunday?


SELECT TRUNC(SYSDATE-7,'DAY')FROM DUAL;

4. How to find the First day of Current Month?


SELECT TRUNC(SYSDATE,'MONTH')FROM DUAL;

5. How to find the First day of Previous Month?


SELECT TRUNC(SYSDATE-1,'MONTH')FROM DUAL;

6. How to find the First day of Next Month?


SELECT TRUNC(ADD_MONTHS(SYSDATE,1),'MON')FROM DUAL;

7. How to find the First day of Current Year?


SELECT ADD_MONTHS(TRUNC(SYSDATE,'YEAR')) FROM DUAL;

8.How to find the First day of Previous Year?


SELECT add_months(trunc(sysdate,'yyyy'),-12) FROM DUAL;

9. How to find the First Day of Current Quarter?


SELECT TRUNC(SYSDATE,'Q') FROM DUAL;

10. How to find the First Day of Previous Quarter?


SELECT TRUNC(ADD_MONTHS(SYSDATE,-3),'Q') AS FIRST FROM DUAL;

11. How to find the First Day of Next Quarter?


SELECT TRUNC(ADD_MONTHS(SYSDATE,+3),'Q') AS FIRST FROM DUAL;

12. How to find the Last day of Current Week, i.e. Saturday?
SELECT TRUNC(SYSDATE,'DAY')+6 FROM DUAL;

13. How to find the Last day of Next Week?


SELECT TRUNC(SYSDATE,'DAY')+13 FROM DUAL;

14. How to find the Last day of Previous Week?


SELECT TRUNC (SYSDATE,'DAY')-1 FROM DUAL;

15. How to find the Last day of Current Month?


SELECT LAST_DAY(SYSDATE) FROM DUAL;

16. How to find the Last day of Previous Month?


SELECT LAST_DAY(ADD_MONTHS(SYSDATE,-1)) FROM DUAL;

17. How to find the Last day of Next Month?


SELECT LAST_DAY(ADD_MONTHS(SYSDATE,+1)) FROM DUAL;

18. How to find the Last day of Current Year?


SELECT LAST_DAY(ADD_MONTHS(SYSDATE,+5)) FROM DUAL;_
19. How to find the Last day of Previous Year?
SELECT LAST_DAY(ADD_MONTHS(SYSDATE,-7)) FROM DUAL;

20. How to find the Last day of Next Year?


SELECT LAST_DAY(ADD_MONTHS(SYSDATE,17)) FROM DUAL;

21. How to find the Last Day of Current Quarter?

SELECT LAST_DAY(TRUNC(ADD_MONTHS(SYSDATE,+2),'Q')+85) AS LAST FROM DUAL;

22. How to find the Last Day of Previous Quarter?

SELECT LAST_DAY(TRUNC(ADD_MONTHS(SYSDATE,-1),'Q')+85) AS LAST FROM DUAL;


23. How to find the Last Day of Next Quarter?
SELECT LAST_DAY(TRUNC(ADD_MONTHS(SYSDATE,3),'Q')+85) AS LAST FROM DUAL;

24. How to get number of days in Current Month?


SELECT TO_CHAR(LAST_DAY(SYSDATE), 'DD') NUMBER_OF_DAYS FROM DUAL;

25. How to get number of days left in Current Month?


SELECT LAST_DAY(SYSDATE)-SYSDATE NUMBER_OF_DAYS_LEFT FROM DUAL;

26. How to get number of days between two dates?


SELECT TO_DATE('02-07-2022','DD-MM-YYYY')-TO_DATE('01-07-2022','DD-MM-YYYY') FROM
DUAL;

27. To display each month Start and End date upto last month of the year?

28. How to get number of seconds passed since today (since 00:00 hr.)?
SELECT (SYSDATE-TRUNC(SYSDATE))*24*60*60 NUM_OF_SEC_SINCE_MORNING FROM DUAL;

29. How to get number of seconds left today (till 23:59:59 hr.)?
SELECT (TRUNC(SYSDATE+1)-(SYSDATE))*24*60*60 NUM_OF_SEC_LEFT FROM DUAL;

30. How to find the First day of Next Year?


SELECT add_months(trunc(sysdate,'yyyy'),12) FROM DUAL;

You might also like