Categories: SQL Complex Queries

What are SQL Date Functions Examples | Industry examples for SQL Date Functions

In my previous articles I have given the basic sql queries with real life examples.In this article I would like to give you SQL Date Functions with real life industry examples. I will explain the different SQL Queries with date functions so that user will get idea about it.These SQL date functions examples are really very important for industry.

Example 1 : First day of Current Week, i.e. Sunday?

 SELECT TO_CHAR (TRUNC (SYSDATE, ‘DAY’), ‘D’) AS DAY_IN_NO,

 TO_CHAR (TRUNC (SYSDATE,’DAY’), ‘DAY’) AS DAY_IN_WORDS,

 TRUNC (SYSDATE, ‘DAY’) AS DAY_DATE FROM DUAL;

Example 2 : First day of Next Week, i.e. Sunday?

 SELECT TO_CHAR (TRUNC (SYSDATE, ‘DAY’), ‘D’) AS DAY_IN_NO,

TO_CHAR (TRUNC (SYSDATE,’DAY’), ‘DAY’) AS DAY_IN_WORDS,

 TRUNC (SYSDATE+7, ‘DAY’) AS DAY_DATE FROM DUAL;

Example 3 : First day of Previous Week, i.e. Sunday?

SELECT TO_CHAR (TRUNC (SYSDATE, ‘DAY’), ‘D’) AS DAY_IN_NO,

TO_CHAR (TRUNC (SYSDATE,’DAY’), ‘DAY’) AS DAY_IN_WORDS,

 TRUNC (SYSDATE-7, ‘DAY’) AS DAY_DATE FROM DUAL;

Example 4 : First day of Current Month?

SELECT TRUNC (SYSDATE,’MM’) FROM DUAL;

Example 5 : First day of Previous Month?

SELECT TRUNC (TRUNC (SYSDATE,’MM’)-1, ‘MM’) FROM DUAL;

SELECT TRUNC (ADD_MONTHS (SYSDATE,-1),’MM’) FROM DUAL;

Example 6 : First day of Next Month?

SELECT TRUNC (LAST_DAY (SYSDATE) +1, ‘MM’) FROM DUAL;

SELECT TRUNC (ADD_MONTHS (SYSDATE, +1),’MM’) FROM DUAL;

Example 7 : First day of Current Year?

SELECT TRUNC (SYSDATE,’YY’) FROM DUAL;

Example 8 : First day of Previous Year?

SELECT TRUNC (TRUNC (SYSDATE,’YY’)-1, ‘YY’) FROM DUAL;

Example 9 : First day of Next Year?

SELECT TRUNC (ADD_MONTHS (SYSDATE, 12),’YY’) FROM DUAL;

SELECT ADD_MONTHS (TRUNC (SYSDATE, ‘YY’), 12) FROM DUAL;

Example 9 : First Day of Current Quarter?

 SELECT TRUNC (SYSDATE, ‘Q’) FROM DUAL;

Example 10 : First Day of Previous Quarter?

SELECT TRUNC (ADD_MONTHS (SYSDATE, -3), ‘Q’) FROM DUAL;

 SELECT ADD_MONTHS (TRUNC (SYSDATE, ‘Q’),-3) FROM DUAL;

Example 11 : First Day of Next Quarter?

SELECT TRUNC (ADD_MONTHS (SYSDATE, 3), ‘Q’) FROM DUAL;

SELECT ADD_MONTHS (TRUNC (SYSDATE, ‘Q’), 3) FROM DUAL;

Example 12 : Last day of Current Week, i.e. Saturday?

 SELECT TRUNC (SYSDATE, ‘DAY’) +6 FROM DUAL;

Example 13 : Last day of Next Week?

 SELECT TRUNC (SYSDATE+7, ‘DAY’) +6 FROM DUAL;

Example 14 : Last day of Previous Week?

SELECT TRUNC (SYSDATE-7, ‘DAY’) +6 FROM DUAL;

Example 15 : Last day of Current Month?

SELECT LAST_DAY (TRUNC (SYSDATE, ‘MM’)) FROM DUAL;

SELECT TRUNC (ADD_MONTHS (SYSDATE, 1), ‘MM’)-1 FROM DUAL;

Example 16 : Last day of Previous Month?

 SELECT TRUNC (SYSDATE, ‘MM’)-1 FROM DUAL;

SELECT LAST_DAY (TRUNC (SYSDATE, ‘MM’)-1) FROM DUAL;

Example 17 : Last day of Next Month?

SELECT LAST_DAY (TRUNC (ADD_MONTHS (SYSDATE, 1),’MM’)) FROM DUAL;

SELECT LAST_DAY (TRUNC (LAST_DAY (SYSDATE)+1, ‘MM’)) FROM DUAL;

Example 18 : Last day of Current Year?

SELECT TRUNC (ADD_MONTHS (SYSDATE, 12),’YY’)-1 FROM DUAL;

SELECT LAST_DAY (ADD_MONTHS (TRUNC (SYSDATE, ‘YY’), 11)) FROM DUAL;

Example 19 : Last day of Previous Year?

SELECT TRUNC (SYSDATE, ‘YY’)-1 FROM DUAL;

SELECT ADD_MONTHS (TRUNC (ADD_MONTHS (SYSDATE,-12),’YY’), 12)-1 FROM DUAL;

Example 20 : Last day of Next Year?

SELECT ADD_MONTHS (TRUNC (ADD_MONTHS (SYSDATE, 12), ‘YY’), 12)-1 FROM DUAL;

Example 21 : Last Day of Current Quarter?

SELECT ADD_MONTHS (TRUNC (SYSDATE,’Q’), 3)-1 FROM DUAL;

SELECT LAST_DAY (ADD_MONTHS (TRUNC (SYSDATE, ‘Q’), 2)) FROM DUAL;

Example 22 : Last Day of Previous Quarter?

 SELECT TRUNC (SYSDATE, ‘Q’)-1 FROM DUAL;

Example 23 : Last Day of Next Quarter?

 SELECT ADD_MONTHS (ADD_MONTHS (TRUNC (SYSDATE, ‘Q’), 3), 3)-1 FROM DUAL;

SELECT LAST_DAY (ADD_MONTHS (TRUNC (SYSDATE, ‘Q’), 5)) FROM DUAL;

Example 24 : Get number of days in Current Month?

 SELECT TO_CHAR (LAST_DAY (SYSDATE),’DD’) FROM DUAL;

SELECT CAST (TO_CHAR (LAST_DAY (SYSDATE), ‘DD’) AS INT) AS NOS_OF_DAYS_IN_MNTH FROM DUAL;

Example 25 : Get number of days left in Current Month?

SELECT TO_CHAR (LAST_DAY (SYSDATE),’DD’) AS LAST_DAY,

TO_CHAR (SYSDATE,’DD’) AS CURRENT_DAY, TO_CHAR (LAST_DAY (SYSDATE),’DD’) – TO_CHAR (SYSDATE,’DD’) AS DAYS_DIFF

FROM DUAL;

            OR

SELECT LAST_DAY (SYSDATE) AS LAST_DAY, SYSDATE,

LAST_DAY (SYSDATE) – SYSDATE AS DAYS_LEFT

FROM DUAL;

Example 26 : Get number of days between two dates?

 SELECT TO_DATE (’31-JAN-2016′,’DD-MON-YYYY’) – TO_DATE (‘1-JAN-2016′,’DD-MON-YYYY’) AS DAYS_BETWEEN_TWO_DATES FROM DUAL;

            OR

SELECT ROUND (MONTHS_BETWEEN (’31-JAN-2016′, ’01-JAN-2016′)*30, 0) DAYS_BETWEEN_TWO_DATES FROM DUAL;

OR

SELECT CEIL (MONTHS_BETWEEN (’31-JAN-2016′, ’01-JAN-2016′)*30) DAYS_BETWEEN_TWO_DATES FROM DUAL;

OR

SELECT TRUNC (SYSDATE) – TO_DATE (‘1-MAY-2016′,’DD-MON-YYYY’) AS DAYS_BETWEEN_TWO_DATES FROM DUAL;

Example 27 : Display each month Start and End date upto last month of the year?

SELECT ALL_MONTHS AS START_DATE_MONTH,

TO_CHAR (LAST_DAY (ALL_MONTHS),’DD-MON-YY’) AS END_DATE_MONTH

FROM

(SELECT TO_CHAR (ADD_MONTHS (’01-JAN-2016′, LEVEL-1),’DD-MON-YY’) AS ALL_MONTHS FROM DUAL CONNECT BY LEVEL <= 12);

Example 28 : Get number of seconds passed since today (since 00:00 hr.)?

SELECT (SYSDATE – TRUNC (SYSDATE)) * 24 * 60 * 60 NOS_OF_SECS_FROM_MORNING FROM DUAL;

Example 29 : Get number of seconds left today (till 23:59:59 hr.)?

SELECT (TRUNC (SYSDATE+1) – SYSDATE) *24 *60 *60 AS NOS_OF_SECS_LEFT FROM DUAL;

These are some most important SQL Date Functions Examples.I hope you will like the SQL Date Functions Examples article.If you like this article or if you have some concerns with the same kindly comment in comments section.

Amit S

Oracle Consultant with vast experience in Oracle BI and PL/SQL Development. Amiet is the admin head of this website who contributes by preparing tutorials and articles related to database technologies. He is responsible to manage the content and front-end of the website.

Share
Published by
Amit S

Recent Posts

What is Root Cause Analysis (RCA) With real examples

In my previous article I have given details about application support engineer day to day…

1 month ago

Application Support Engineer Day to day responsibilities

In my previous articles I have given the roles and responsibilities of L1,L2 and L3…

1 month ago

What is mean by SLA ( Service Level Agreement) with Examples?

In my previous articles i have given the hierarchy of production support in real company…

1 month ago

What is Production support Hierarchy in organization?

In this article i would like to provide information about production support organization structure or…

1 month ago

What are roles and responsibilities for L3 Support Engineer?

In my previous article I have given roles for L1 and L2 support engineer with…

1 month ago

What are roles and responsibilities of L2 Engineer?

I have started this new series of how to become application support engineer. This article…

1 month ago