0% found this document useful (0 votes)
3 views6 pages

Derive Attrb

The document provides a comprehensive list of SQL date functions, including their descriptions and syntax. Key functions such as DAY, MONTH, YEAR, EOMONTH, DATEADD, DATEDIFF, and DATEDIFF_BIG are detailed with examples for practical application. This serves as a quick reference guide for working with dates in T-SQL coding.

Uploaded by

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

Derive Attrb

The document provides a comprehensive list of SQL date functions, including their descriptions and syntax. Key functions such as DAY, MONTH, YEAR, EOMONTH, DATEADD, DATEDIFF, and DATEDIFF_BIG are detailed with examples for practical application. This serves as a quick reference guide for working with dates in T-SQL coding.

Uploaded by

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

olution

Below is a list of SQL Date functions with descriptions, followed by several examples of
each. Hopefully, this will serve as a good quick reference when working with dates in T-
SQL coding.

List of SQL DATE Functions


The DATE functions in the following table will be presented in this article.

Return Value Data


Date Functions Desc Note
Type
Returns the day of the week
DAY (date or datetime) Integer like 1 - 31
for a given date
MONTH (date or Returns the month of a given
Integer like 1 - 12
datetime) date
Returns the year of a given
YEAR (date or datetime) Integer for year like 2021
date
Integer like 1 – 12 for
DATEPART (date part, Returns the date part specified
month, 1 – 31 for day, or
date or datetime) in int format
year like 2021
Character like April, May,
DATENAME (date part, Returns the date part specified
‘1’, ‘2’, ‘31’, ‘2020’,
date or datetime) in character format
‘2021’
Returns the last do of the
EOMONTH (date month with an optional Introduced in SQL Server
01/31/2021
[,months to add) parameter to add months (+ or 2012.
-).
DATEADD (date part,
Return date math results datetime
units, date or datetime)
Give the difference between 2
DATEDIFF (date part,
dates in units specified by date Integer of date part units
start date, end date)
part
Give the difference between 2
Big Integer of date part
DATEDIFF_BIG dates in units specified by date
units
part
Typically, a character *To convert a valid date char
CONVERT (date type, Used to convert date output to
datatype is specified when string to date no function is
value [ , style ] a specified mask
converting dates. needed! Implicit convert!
Returns a date formatted
FORMAT ( value, format Used to convert date output to
string based on the mask
[, culture ] ) a specified mask
specified.
Used to convert different data
CAST (value as data Returns data in the data
types to a date or datetime data
type) type specified.
type.
Returns 1 if the string is a
ISDATE (potential date
Use to validate a date string valid date or 0 if not a
string)
valid date.

Date Function DAY()


The date function DAY accepts a date, datetime, or valid date string and returns the
Day part as an integer value.

Syntax: DAY(date)

--Example of DAY():
SELECT GETDATE(), DAY(GETDATE()) , DAY('20210101'), DAY('2021-05-30
15:46:19.277');
GO

Results:

Date Function MONTH()


The date function MONTH accepts a date, datetime, or valid date string and returns the
Month part as an integer value.

Syntax: MONTH(date)

--Example of MONTH():
SELECT GETDATE(), MONTH(GETDATE()) , MONTH('20210101'), MONTH('2021-05-30
15:46:19.277');
GO

Results:

Date Function YEAR()


The date function YEAR accepts a date, datetime, or valid date string and returns the
Year part as an integer value.
Syntax: YEAR(date)

--Example of YEAR():
SELECT GETDATE(), YEAR(GETDATE()) , YEAR('20210101'), YEAR('2021-05-30
15:46:19.277');
GO

Results:

Date Function EOMONTH()


The date function EOMONTH accepts a date, datetime, or valid date string and returns
the end of month date as a datetime. It can also take an optional offset that basically
adds or subtracts months from the current passed date.

Syntax: EOMONTH(start_date [, month_to_add ])

--Example of EOMONTH(): Shows different date formats being passed in.


SELECT EOMONTH(GETDATE()), EOMONTH('20210101'), EOMONTH('May 1, 2021');

--Example of EOMONTH(): Shows the use of the offset optional parameter with
the GETDATE function which is the current date
SELECT EOMONTH(GETDATE()) as 'End Of Current Month',
EOMONTH(GETDATE(),-1) as 'End Of Last Month',
EOMONTH(GETDATE(),6) as 'End Of Month +6';
GO

Results:

Date Function DATEADD


The date function DATEADD accepts a date part, a number to add, date, datetime, or
valid date string and returns datetime result based on the units add (can be negative).

Syntax: DATEADD(date part, units, date or datetime)

Date Parts: can use the name or listed abbreviations:

 year, yy, yyyy


 quarter, qq, q
 month, mm, m
 dayofyear, dy, y*
 day, dd, d*
 weekday, dw, w*
 week, wk, ww
 hour, hh
 minute, mi, n
 second, ss, s
 millisecond, ms
 microsecond, mcs
 nanosecond, ns

*Note: dayofyear, day, and weekday return the same value.

--Example of DATEADD():
SELECT DATEADD(DAY,1,'2021-01-01') as 'Add 1 Day',
DATEADD(WEEK,1,'2021-01-01') as 'Add 1 Week',
DATEADD(MONTH,1,'2021-01-01') as 'Add 1 Month',
DATEADD(YEAR,1,'2021-01-01') as 'Add 1 Year';
GO

Results:

Date Function DATEDIFF


The date function DATEDIFF accepts a date part, start date and end date as date
datetime, or valid date string and returns the difference between the dates in units bast
on the date part specified.

Syntax: DATEDIFF (date part, start date, end date)

Date Parts: can use the name or listed abbreviations:


 year, yy, yyyy
 quarter, qq, q
 month, mm, m
 dayofyear, dy, y
 day, dd, d
 week, wk, ww
 hour, hh
 minute, mi, n
 second, ss, s
 millisecond, ms
 microsecond, mcs
 nanosecond, ns

*The following example will use the date part names rather than the abbreviation.

--Example of DATEDIFF():
SELECT DATEDIFF(DAY,'2021-01-01','2021-02-01') as 'Number of Days in January',
DATEDIFF(WEEK,'2021-01-01','2022-01-01') as 'Weeks in the Year',
DATEDIFF(MONTH,'2021-01-01','2022-01-01') as 'Months in the Year',
DATEDIFF(YEAR,'1900-01-01',GETDATE()) as 'Years Since 1900';
GO

Results:

To learn more on DATEDIFF Read tip: DATEDIFF SQL Server Function.

Date Function DATEDIFF_BIG


The DATEDIFF_BIG function is used in the same way as the DATEDIFF function.
However, the DATEDIFF_BIG function is typically used with date parts: millisecond,
microsecond, and nanosecond when the return value exceeds the range of integer (-
2,147,483,648 to +2,147,483,647) thus requiring the return value as a BIGINT data
type.

Syntax: DATEDIFF_BIG(date part, start date, end date)

Date Parts: can use the name or listed abbreviations:

 year, yy, yyyy


 quarter, qq, q
 month, mm, m
 dayofyear, dy, y
 day, dd, d
 week, wk, ww
 hour, hh
 minute, mi, n
 second, ss, s
 millisecond, ms
 microsecond, mcs
 nanosecond, ns

*The following example will use the datepart names.

--Example of DATEDIFF_BIG():
SELECT DATEDIFF_BIG(MILLISECOND, '01-01-2020', '01-01-2021') as 'Milliseconds
in a Year'
SELECT DATEDIFF_BIG(NANOSECOND, '01-01-2020', '01-01-2021') as 'Nanoseconds in
a Year'
GO

Results:

You might also like