Date Functions
Table 2-19 Date Functions in Oracle and Microsoft SQL Server and Sybase Adaptive Server
Microsoft SQL Server
and Sybase Adaptive
Server Oracle Description
dateadd(dd, int_ date+int_exp Adds the int_exp number of days to the date
exp,datetime_var) requires
contained in datetime_var.
conversion of
int_exp to a
number of
days
dateadd(mm, int_ add_months(date, Adds the int_exp number of months to the
exp,datetime_var) int_
date contained in datetime_var.
exp)
or
date+int_exp
requires
conversion of
int_exp to
a number of days
dateadd(yy, int_ date+int_exp Adds the int_exp number of years to the date
exp,datetime_var) requires
contained in datetime_var.
conversion of
int_exp to a
number of
days
datediff(dd, date2-date1 Returns the difference between the dates
datetime1,datetime2)
specified by the datetime1 and datetime2
variables. This difference is calculated in the
number of days.
datediff(mm, months_between Returns the difference between the dates
datetime1,datetime2) ( date2, date1)
specified by the datetime1 and datetime2
variables. This difference is calculated in the
number of months.
datediff(yy, (date2-date1) Returns the difference between the dates
datetime1,datetime2) /365.254
specified by the datetime1 and datetime2
variables. This difference is calculated in the
number of years.
datename (datepart, to_char(date, Returns the specified part of the date as an
date) format)
integer. The Microsoft SQL Server and
Sybase Adaptive Server DATETIME has a
higher precision than Oracle DATE. For this
reason, it is not always possible to find an
equivalent format string in Oracle to match
the datepart in Microsoft SQL Server and
Microsoft SQL Server
and Sybase Adaptive
Server Oracle Description
Sybase Adaptive Server. See the Data Types
section of this chapter for more information
about conversion of the DATETIME data
type.
datepart(datepart, to_char(date, Returns the specified part of the date as a
date) format)
character string (name). The Microsoft SQL
Server and Sybase Adaptive Server
DATETIME has a higher precision than
Oracle DATE'. For this reason, it is not
always possible to find an equivalent format
string in Oracle to match the datepart in
Microsoft SQL Server and Sybase Adaptive
Server.
getdate() sysdate Returns the system date.
Recommendations:
The above table lists all the Microsoft SQL Server and Sybase Adaptive Server date
manipulation functions. It does not list all the Oracle date functions. There are many more Oracle
date manipulation functions that you can use.
It is recommended that you convert most date manipulation functions to "+" or "-" in Oracle.
Oracle adds the ability to define functions. With this feature you can create Oracle functions that
match the name and functionality of all Microsoft SQL Server and Sybase Adaptive Server
functions. This is a useful feature, where users can call a PL/SQL function from a SQL
statement's SELECT LIST, WHERE clause, ORDER BY clause, and HAVING clause. With the
parallel query option, Oracle executes the PL/SQL function in parallel with the SQL statement.
Hence, users create parallel logic.
Oracle date functions
Oracle has a number of functions that apply to a date
Sysdate Returns the current date/time
ADD_MONTHS Function to add a number of months to a date. For example:
add_months(SYSDATE,3) returns 3 months after sysdate. This could be
rounded to below is the resulting month has fewer days than the month
this function is applied to.
In Oracle you can add or substract a number of days from a date. Example:
+,- (plus/minus)
sysdate+5 means systemdate/time plus 5 days
With the greatest function you can select the date/time that is the highest
GREATEST in a range of date/times. Example: greatest (sysdate+4,sysdate,sysdate-5)
= sysdate+4.
With the least function you can select the earliest date/time in a range of
LEAST
date/times. Example: least(sysdate+4,sysdate,sysdate-5) = sysdate-5.
Returns the last_day of a month based on the month the passed date is in.
LAST_DAY
Example: last_day(sysdate) returns the last day of this month.
Returns the number of months between two dates. The number is not
MONTHS_BETWEEN rounded. Example: months_between(sysdate, to_date('01-01-2007','dd-
mm-yyyy')) returns the number of months since jan 1, 2007.
Date of next specified date following a date NEXT_DAY(, ) Options are
NEXT_DAY SUN, MON, TUE, WED, THU, FRI, and SAT SELECT NEXT_DAY(SYSDATE,
'FRI') FROM dual; NOTE: This can be dependend on NLS_SETTINGS!
Returns date rounded to the unit specified by the format model. If you
ROUND omit the format, the date is rounded to the nearest day ROUND(, ) SELECT
ROUND(TO_DATE('27-OCT-00'),'YEAR') NEW_YEAR FROM dual;
Convert a date to the date without time (0:00h) Example: TRUNC(sysdate)
TRUNC
returns today without time.
trunc(example_date,'MM') Example: select trunc(TO_DATE('31-JAN-
First day of the month.
2007'),'MM') FROM dual;
Converts a date to a string using a format mask. Format masks are
TO_CHAR(date,format_mask)
explained here
Format Mask
Rounds or Truncates to
CC or SSC - Century
SYYY, YYYY, YEAR, SYEAR, YYY, YY, or Y - Year (rounds up to next year on July 1)
IYYY, IYY, IY, or I - Standard ISO year
Q - Quarter (rounds up on the sixteenth day of the second month of the quarter)
MONTH, MON, MM, or RM - Month (rounds up on the sixteenth day, which is not necessarily
the same as the middle of the month)
WW - Same day of the week as the first day of the year
IW - Same day of the week as the first day of the ISO year
W - Same day of the week as the first day of the month
DDD, DD, or J - Day
DAY, DY, or D - Starting day of the week
HH, HH12, HH24 - Hour
MI - Minute
NEW_TIME - Returns the date/time value, with the time shifted as requested by the specified
time zones
Time Zone Abbreviation Description
AST Atlantic Standard Time
ADT Atlantic Daylight Time
BST Bering Standard Time
BDT Bering Daylight Time
CST Central Standard Time
CDT Central Daylight Time
EST Eastern Standard Time
EDT Eastern Daylight Time
GMT Greenwich Mean Time
HST Alaska-Hawaii Standard Time
HDT Alaska-Hawaii Daylight Time
MST Mountain Standard Time
MDT Mountain Daylight Time
NST Newfoundland Standard Time
PST Pacific Standard Time
PDT Pacific Daylight Time
YST Yukon Standard Time
YDT Yukon Daylight Time
SQL Date Functions
CURRENT_DATE ( )
Returns the current date.
CURRENT_TIME ()
Returns the current local time.
CURRENT_TIMESTAMP ()
Returns the current local date and local time as a timestamp value.
CURDATE ( )
Returns the current date.
CURTIME ( )
Returns the current local time.
DAYNAME (date_exp)
Returns a character string containing the data source/specific name of the day (for example, Sunday
through Saturday).
DAYOFMONTH (date_exp)
Returns the day of the month based on the month field in date_exp as an integer value in the range of
1-31.
DAYOFWEEK (date_exp)
Returns the day of the week based on the week field in date_exp as an integer value in the range of 1-
7, where 1 represents Sunday.
DAYOFYEAR(date_exp)
Returns the day of the year based on the year field in date_exp as an integer value in the range of 1-
366.
EXTRACT (extract-field FROM extract-source)
Returns the extract-field portion of the extract-source. The extract-source argument is a datetime or
interval expression.
HOUR (time_exp)
Returns the hour based on the hour field in time_exp as an integer value in the range of 0-23.
MINUTE (time_exp)
Returns the minute based on the minute field in time_exp as an integer value in the range of 0-59.
MONTH (date_exp)
Returns the month based on the month field in date_exp as an integer value in the range of 1-12.
MONTHNAME(date_exp)
Returns a character string containing the data source/specific name of the month (for example,
January through December). Currently only supports English locale.
NOW ( )
Returns current date and time as a timestamp value.
QUARTER(date_exp)
Returns the quarter in date_exp as an integer value in the range of 1-4, where 1 represents January 1
through March 31.
SECOND (time_exp)
Returns the second based on the second field in time_exp as an integer value in the range of 0-59.
TIMESTAMPADD (interval, integer_exp, timestamp_exp)
Returns a timestamp offset by the specified interval.
Interval can be one of the following values
SQL_TSI_FRAC_SECOND (in billionths of a second)
SQL_TSI_SECOND
SQL_TSI_MINUTE
SQL_TSI_HOUR
SQL_TSI_DAY
SQL_TSI_WEEK
SQL_TSI_MONTH
SQL_TSI_QUARTER
SQL_TSI_YEAR
Example:
SELECT timestampadd(SQL_TSI_HOUR, 3, {ts “1998-01-11 10:00:00”})
Returns: 1998-01-01 13:00:00
Example:
SELECT timestampadd(SQL_TSI_DAY, -3, {ts “1998-01-11 10:00:00”})
Returns: 1998-01-08 10:00:00
TIMESTAMPDIFF (interval, timestamp_exp1, timestamp_exp2)
Returns the integer number of intervals of type interval by which timestamp_exp2 is greater than
timestamp_exp1. Valid Intervals are described in the TIMESTAMPADD function.
Example:
SELECT timestampdiff(SQL_TSI_MONTH, {ts “1998-01-11 10:00:00”}, {ts “1998-03-11
10:00:00”})
Returns: -2
Example:
SELECT timestampdiff( SQL_TSI_YEAR, {ts “2003-01-11 10:00:00”}, {ts “1998-01-11
10:00:00”})
Returns: 5
WEEK (date_exp)
Returns the week of the year based on the week field in date_exp as an integer value in the range of
1-53.
YEAR(date_exp)
Returns the year based on the year field in date_exp as an integer value. The range is data source-
dependent.