0% found this document useful (0 votes)
95 views17 pages

Database Programming With SQL: 4-3 Date Functions

The document discusses date functions in SQL including SYSDATE, DATE data type, and functions like MONTHS_BETWEEN, ADD_MONTHS, NEXT_DAY, LAST_DAY, ROUND, and TRUNC. Examples are provided to demonstrate usage and output of each function.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
95 views17 pages

Database Programming With SQL: 4-3 Date Functions

The document discusses date functions in SQL including SYSDATE, DATE data type, and functions like MONTHS_BETWEEN, ADD_MONTHS, NEXT_DAY, LAST_DAY, ROUND, and TRUNC. Examples are provided to demonstrate usage and output of each function.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Database Programming with SQL

4-3
Date Functions

Copyright © 2017, Oracle and/or its affiliates. All rights reserved.


Objectives

This lesson covers the following objectives:


• Demonstrate the use of SYSDATE and date functions
• State the implications for world businesses to be able to
easily manipulate data stored in date format
• Demonstrate the use of SYSDATE and date functions
• State the implications for world businesses to be able to
easily manipulate data stored in date format

DPS4L3
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 2
Date Functions
Displaying Dates

• The default display and input format for


dates is: DD-Mon-YYYY
• For example: 02-Dec-2014.
• However, the Oracle database stores dates internally with a
numeric format representing the century, year, month, day,
hour, minute, and second.
• Valid Oracle dates are between January 1, 4712 B.C., and
December 31, 9999 A.D.
• This represents the range of dates that you can store
successfully in an Oracle database.

DPS4L3
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 3
Date Functions
SYSDATE

• SYSDATE is a date function that returns the current database


server date and time.
• Use SYSDATE to display the current date, use the DUAL table.
SELECT SYSDATE SYSDATE
FROM dual;
01-Jul-2017

DPS4L3
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 4
Date Functions
DATE Data Type

• The DATE data type always stores year information as a four-


digit number internally: two digits for the century and two
digits for the year.
• For example, the Oracle database stores the year as 1996 or
2004, not just as 96 or 04.
• In previous versions, the century component was not
displayed by default.
• However, due to changing business requirements around the
world, the 4-digit year is now the default display.

DPS4L3
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 5
Date Functions
Working with Dates

Examples: Result
SELECT last_name, hire_date + 60 Adds 60 days to
FROM employees; hire_date.
SELECT last_name, (SYSDATE - Displays the number of
hire_date)/7 weeks since the employee
FROM employees; was hired.
SELECT employee_id, (end_date - Finds the number of days
start_date)/365 employee held a job, then
AS "Tenure in last job"
divides by 365 to display
FROM job_history;
in years.

DPS4L3
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 6
Date Functions
Date Functions

• The date functions shown in the table operate on Oracle


dates.
• All of the date functions return a value with a DATE data type
except the MONTHS_BETWEEN function, which returns a
numeric data type value.
Function Description
MONTHS_BETWEEN Number of months between two dates
ADD_MONTHS Add calendar months to date
NEXT_DAY Date of the next occurance of day of the week specified
LAST_DAY Last day of the month
ROUND Round date
TRUNC Truncate date

DPS4L3
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 7
Date Functions
Date Functions

• MONTHS_BETWEEN: takes 2 DATE arguments and returns the


number of calendar months between the 2 dates.
• If the first argument is an earlier date than the second, the
number returned is negative.
Date Function Examples: Result
SELECT last_name, hire_date King 17-Jun-1987
FROM employees Kochhar 21-Sep-1989
WHERE MONTHS_BETWEEN De Haan 13-Jan-1993
(SYSDATE, hire_date) > 240;
… …

DPS4L3
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 10
Date Functions
Date Functions

• ADD_MONTHS: takes 2 arguments, a DATE and a number.


Returns a DATE value with the number argument added to
the month component of the date.
• If the number supplied is negative, the function will subtract
that number of months from the date argument.
Date Function Examples: Result
SELECT ADD_MONTHS (SYSDATE, 12) 01-Jul-2016
AS "Next Year"
FROM dual;

DPS4L3
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 9
Date Functions
Date Functions

• NEXT_DAY: takes 2 arguments, a DATE and a weekday and


returns the DATE of the next occurrence of that weekday
after the DATE argument.
Date Function Examples: Result
SELECT NEXT_DAY (SYSDATE, 'Saturday') 04-Jul-2017
AS "Next Saturday"
FROM dual;

DPS4L3
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 10
Date Functions
Date Functions

• LAST_DAY: takes a DATE argument and returns the DATE of


the last day of the month for the DATE argument.
Date Function Examples: Result
SELECT LAST_DAY (SYSDATE) 31-Jul-2017
AS "End of the Month"
FROM dual;

DPS4L3
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 11
Date Functions
Date Functions

• ROUND: returns a DATE rounded to the unit specified by the


second argument.
Date Function Examples: Result
SELECT hire_date, 16 Nov-1999 01-Dec-1999
ROUND(hire_date, 'Month') 17 Oct-1995 01-Nov-1995
FROM employees
29-Jan-1997 01-Feb-1997
WHERE department_id = 50;
… …
SELECT hire_date, 16 Nov-1999 01-Jan-2000
ROUND(hire_date, 'Year') 17 Oct-1995 01-Jan-1996
FROM employees
29-Jan-1997 01-Jan-1997
WHERE department_id = 50;
… …

DPS4L3
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 12
Date Functions
Date Functions

• TRUNC: returns a DATE truncated to the unit specified by the


second argument.
Date Function Examples: Result
SELECT hire_date, 16 Nov-1999 01-Nov-1999
TRUNC(hire_date, 'Month') 17 Oct-1995 01-Oct-1995
FROM employees
29-Jan-1997 01-Jan-1997
WHERE department_id = 50;
… …
SELECT hire_date, 16 Nov-1999 01-Jan-1999
TRUNC(hire_date, 'Year') 17 Oct-1995 01-Jan-1995
FROM employees
29-Jan-1997 01-Jan-1997
WHERE department_id = 50;
… …

DPS4L3
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 13
Date Functions
Date Functions

• Here is an example of a query using multiple date functions.


• The output is displayed on the next slide.
SELECT employee_id, hire_date,
ROUND(MONTHS_BETWEEN(SYSDATE, hire_date)) AS TENURE,
ADD_MONTHS (hire_date, 6) AS REVIEW,
NEXT_DAY(hire_date, 'FRIDAY'), LAST_DAY(hire_date)
FROM employees
WHERE MONTHS_BETWEEN (SYSDATE, hire_date) > 36;

DPS4L3
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 14
Date Functions
Date Functions

• The result set from this query returns 20 rows including:


EMPLOYEE_ID HIRE_DATE TENURE REVIEW NEXT_DAY(HIRE_DATE,'FRIDAY') LAST_DAY(HIRE_DATE)

100 17-Jun-1987 348 17-Dec-1987 19-Jun-1987 30-Jun-1987

101 21-Sep-1989 321 21-Mar-1990 22-Sep-1989 30-Sep-1989

102 13-Jan-1993 281 13-Jul-1993 15-Jan-1993 31-Jan-1993

200 17-Sep-1987 345 17-Mar-1988 18-Sep-1987 30-Sep-1987

205 07-Jun-1994 265 07-Dec-1994 10-Jun-1994 30-Jun-1994

206 07-Jun-1994 265 07-Dec-1994 10-Jun-1994 30-Jun-1994

149 29-Jan-2000 197 29-Jul-2000 04-Feb-2000 31-Jan-2000

174 11-May-1996 241 11-Nov-1996 17-May-1996 31-May-1996

176 24-Mar-1998 219 24-Sep-1998 27-Mar-1998 31-Mar-1998

178 24-May-1999 205 24-Nov-1999 28-May-1999 31-May-1999


More than 10 rows available. Increase rows selector to view more rows.

DPS4L3
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 15
Date Functions
Summary

In this lesson, you should have learned how to:


• Select and apply the single-row functions
MONTHS_BETWEEN, ADD_MONTHS, NEXT_DAY, LAST_DAY,
ROUND, and TRUNC that operate on date data
• Explain how date functions transform Oracle dates into date
data or numeric values
• Demonstrate proper use of the arithmetic operators with
dates

DPS4L3
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 16
Date Functions
Summary

In this lesson, you should have learned how to:


• Demonstrate the use of SYSDATE and date functions
• State the implications for world businesses to be able to
easily manipulate data stored in date format

DPS4L3
Copyright © 2017, Oracle and/or its affiliates. All rights reserved. 20
Date Functions

You might also like