0% found this document useful (0 votes)
76 views

Date and Time Data Types and Functions - SQL Server (Transact-SQL) - Microsoft Learn PDF

The document discusses the date and time data types and functions available in SQL Server Transact-SQL. It lists the different date and time data types, their formats, ranges, accuracy, storage size, and whether they support user-defined fractional seconds or time zones. It also describes functions that return system date and time values, functions that return date and time parts, functions that modify date and time values, and functions that validate date and time values.

Uploaded by

fatah momen
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)
76 views

Date and Time Data Types and Functions - SQL Server (Transact-SQL) - Microsoft Learn PDF

The document discusses the date and time data types and functions available in SQL Server Transact-SQL. It lists the different date and time data types, their formats, ranges, accuracy, storage size, and whether they support user-defined fractional seconds or time zones. It also describes functions that return system date and time values, functions that return date and time parts, functions that modify date and time values, and functions that validate date and time values.

Uploaded by

fatah momen
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/ 8

2/5/23, 11:31 PM Date and Time Data Types and Functions - SQL Server (Transact-SQL) | Microsoft Learn

Date and time data types and functions


(Transact-SQL)
Article • 12/15/2022 • 7 minutes to read

Applies to: SQL Server Azure SQL Database Azure SQL Managed Instance
Azure Synapse Analytics

The sections in this article cover all Transact-SQL date and time data types and functions.

Date and time data types


Date and time functions
Functions that return system date and time values
Functions that return date and time parts
Functions that return date and time values from their parts
Functions that return date and time difference values
Functions that modify date and time values
Functions that set or return session format functions
Functions that validate date and time values
Date and time-related articles

Date and time data types


The Transact-SQL date and time data types are listed in the following table:

Data type Format Range Accuracy Storage User- Time


size defined zone
(bytes) fractional offset
second
precision

time hh:mm:ss[.nnnnnnn] 00:00:00.0000000 100 3 to 5 Yes No


through nanoseconds
23:59:59.9999999

date YYYY-MM-DD 0001-01-01 1 day 3 No No


through 9999-12-
31

smalldatetime YYYY-MM-DD 1900-01-01 1 minute 4 No No


hh:mm:ss through 2079-06-
06

datetime YYYY-MM-DD 1753-01-01 0.00333 8 No No


hh:mm:ss[.nnn] through 9999-12- second
31

datetime2 YYYY-MM-DD 0001-01-01 100 6 to 8 Yes No


hh:mm:ss[.nnnnnnn] 00:00:00.0000000 nanoseconds
https://learn.microsoft.com/en-us/sql/t-sql/functions/date-and-time-data-types-and-functions-transact-sql?view=sql-server-ver16 1/8
2/5/23, 11:31 PM Date and Time Data Types and Functions - SQL Server (Transact-SQL) | Microsoft Learn

Data type Format Range Accuracy Storage User- Time


size defined zone
(bytes) fractional offset
second
precision

through 9999-12-
31
23:59:59.9999999

datetimeoffset YYYY-MM-DD 0001-01-01 100 8 to 10 Yes Yes


hh:mm:ss[.nnnnnnn] 00:00:00.0000000 nanoseconds
[+|-]hh:mm through 9999-12-
31
23:59:59.9999999
(in UTC)

7 Note

The Transact-SQL rowversion data type is not a date or time data type. timestamp is a
deprecated synonym for rowversion.

Date and time functions


The following tables list the Transact-SQL date and time functions. See Deterministic and
Nondeterministic Functions for more information about determinism.

Functions that return system date and time values


Transact-SQL derives all system date and time values from the operating system of the computer
on which the instance of SQL Server runs.

Higher-precision system date and time functions


Since SQL Server 2008 (10.0.x), the Database Engine derives the date and time values through
use of the GetSystemTimeAsFileTime() Windows API. The accuracy depends on the computer
hardware and version of Windows on which the instance of SQL Server running. This API has a
precision fixed at 100 nanoseconds. Use the GetSystemTimeAdjustment() Windows API to
determine the accuracy.

Function Syntax Return value Return data Determinism


type

SYSDATETIME SYSDATETIME ( ) Returns a datetime2(7) Nondeterministic


datetime2(7)
value containing
the date and time

https://learn.microsoft.com/en-us/sql/t-sql/functions/date-and-time-data-types-and-functions-transact-sql?view=sql-server-ver16 2/8
2/5/23, 11:31 PM Date and Time Data Types and Functions - SQL Server (Transact-SQL) | Microsoft Learn

Function Syntax Return value Return data Determinism


type

of the computer
on which the
instance of SQL
Server runs. The
returned value
doesn't include
the time zone
offset.

SYSDATETIMEOFFSET SYSDATETIMEOFFSET ( ) Returns a datetimeoffset(7) Nondeterministic


datetimeoffset(7)
value containing
the date and time
of the computer
on which the
instance of SQL
Server runs. The
returned value
includes the time
zone offset.

SYSUTCDATETIME SYSUTCDATETIME ( ) Returns a datetime2(7) Nondeterministic


datetime2(7)
value containing
the date and time
of the computer
on which the
instance of SQL
Server is running.
The function
returns the date
and time values
as UTC time
(Coordinated
Universal Time).

Lower-precision system date and time functions

Function Syntax Return value Return Determinism


data
type

CURRENT_TIMESTAMP CURRENT_TIMESTAMP Returns a datetime datetime Nondeterministic


value containing the
date and time of the
computer on which the
instance of SQL Server
runs. The returned value

https://learn.microsoft.com/en-us/sql/t-sql/functions/date-and-time-data-types-and-functions-transact-sql?view=sql-server-ver16 3/8
2/5/23, 11:31 PM Date and Time Data Types and Functions - SQL Server (Transact-SQL) | Microsoft Learn

Function Syntax Return value Return Determinism


data
type

doesn't include the time


zone offset.

GETDATE GETDATE ( ) Returns a datetime datetime Nondeterministic


value containing the
date and time of the
computer on which the
instance of SQL Server
runs. The returned value
doesn't include the time
zone offset.

GETUTCDATE GETUTCDATE ( ) Returns a datetime datetime Nondeterministic


value containing the
date and time of the
computer on which the
instance of SQL Server
runs. The function
returns the date and
time values as UTC time
(Coordinated Universal
Time).

Functions that return date and time parts

Function Syntax Return value Return data Determinism


type

DATE_BUCKET DATE_BUCKET ( Returns a value corresponding to The return Nondeterministic


datepart, the start of each date-time type depends
number, date, bucket from the timestamp on the
origin ) defined by the origin parameter, argument
or the default origin value of supplied for
1900-01-01 00:00:00.000 if the date.
origin parameter isn't specified.

DATENAME DATENAME ( Returns a character string nvarchar Nondeterministic


datepart, date ) representing the specified
datepart of the specified date.

DATEPART DATEPART ( Returns an integer representing int Nondeterministic


datepart, date ) the specified datepart of the
specified date.

DATETRUNC DATETRUNC ( Returns an input date truncated The return Nondeterministic


datepart, date ) to a specified datepart. type depends
on the
argument

https://learn.microsoft.com/en-us/sql/t-sql/functions/date-and-time-data-types-and-functions-transact-sql?view=sql-server-ver16 4/8
2/5/23, 11:31 PM Date and Time Data Types and Functions - SQL Server (Transact-SQL) | Microsoft Learn

Function Syntax Return value Return data Determinism


type

supplied for
date.

DAY DAY ( date ) Returns an integer representing int Deterministic


the day part of the specified date.

MONTH MONTH ( date ) Returns an integer representing int Deterministic


the month part of a specified
date.

YEAR YEAR ( date ) Returns an integer representing int Deterministic


the year part of a specified date.

Functions that return date and time values from their parts

Function Syntax Return value Return data Determinism


type

DATEFROMPARTS DATEFROMPARTS ( year, Returns a date date Deterministic


month, day ) value for the
specified year,
month, and
day.

DATETIME2FROMPARTS DATETIME2FROMPARTS ( Returns a datetime2( Deterministic


year, month, day, hour, datetime2 precision )
minute, seconds, fractions, value for the
precision) specified date
and time, with
the specified
precision.

DATETIMEFROMPARTS DATETIMEFROMPARTS ( year, Returns a datetime Deterministic


month, day, hour, minute, datetime value
seconds, milliseconds) for the
specified date
and time.

DATETIMEOFFSETFROMPARTS DATETIMEOFFSETFROMPARTS Returns a datetimeoffset( Deterministic


( year, month, day, hour, datetimeoffset precision )
minute, seconds, fractions, value for the
hour_offset, minute_offset, specified date
precision) and time, with
the specified
offsets and
precision.

SMALLDATETIMEFROMPARTS SMALLDATETIMEFROMPARTS Returns a smalldatetime Deterministic


( year, month, day, hour, smalldatetime
minute ) value for the

https://learn.microsoft.com/en-us/sql/t-sql/functions/date-and-time-data-types-and-functions-transact-sql?view=sql-server-ver16 5/8
2/5/23, 11:31 PM Date and Time Data Types and Functions - SQL Server (Transact-SQL) | Microsoft Learn

Function Syntax Return value Return data Determinism


type

specified date
and time.

TIMEFROMPARTS TIMEFROMPARTS ( hour, Returns a time time( precision Deterministic


minute, seconds, fractions, value for the )
precision ) specified time,
with the
specified
precision.

Functions that return date and time difference values

Function Syntax Return value Return Determinism


data type

DATEDIFF DATEDIFF ( datepart, Returns the number of date or time int Deterministic
startdate, enddate ) datepart boundaries, crossed
between two specified dates.

DATEDIFF_BIG DATEDIFF_BIG ( Returns the number of date or time bigint Deterministic


datepart, startdate, datepart boundaries, crossed
enddate ) between two specified dates.

Functions that modify date and time values

Function Syntax Return value Return data type Determinism

DATEADD DATEADD (datepart, Returns a new The data type of Deterministic


number, date ) datetime value by the date argument
adding an interval to
the specified datepart
of the specified date.

EOMONTH EOMONTH ( Returns the last day of Return type is the Deterministic
start_date [, the month containing type of the
month_to_add ] ) the specified date, with start_date
an optional offset. argument, or
alternately, the
date data type.

SWITCHOFFSET SWITCHOFFSET SWITCHOFFSET datetimeoffset Deterministic


(DATETIMEOFFSET, changes the time zone with the fractional
time_zone) offset of a precision of the
DATETIMEOFFSET DATETIMEOFFSET
value, and preserves
the UTC value.

TODATETIMEOFFSET TODATETIMEOFFSET TODATETIMEOFFSET datetimeoffset Deterministic


(expression, transforms a datetime2 with the fractional
https://learn.microsoft.com/en-us/sql/t-sql/functions/date-and-time-data-types-and-functions-transact-sql?view=sql-server-ver16 6/8
2/5/23, 11:31 PM Date and Time Data Types and Functions - SQL Server (Transact-SQL) | Microsoft Learn

Function Syntax Return value Return data type Determinism

time_zone) value into a precision of the


datetimeoffset value. datetime
TODATETIMEOFFSET argument
interprets the
datetime2 value in
local time, for the
specified time_zone.

Functions that set or return session format functions

Function Syntax Return value Return Determinism


data type

@@DATEFIRST @@DATEFIRST Returns the current value, for tinyint Nondeterministic


the session, of SET DATEFIRST.

SET DATEFIRST SET DATEFIRST { Sets the first day of the week to Not Not applicable
number | a number from 1 through 7. applicable
@number_var }

SET SET DATEFORMAT { Sets the order of the dateparts Not Not applicable
DATEFORMAT format | (month/day/year) for entering applicable
@format_var } datetime or smalldatetime
data.

@@LANGUAGE @@LANGUAGE Returns the name of the Not Not applicable


language in current used. applicable
@@LANGUAGE isn't a date or
time function. However, the
language setting can affect the
output of date functions.

SET LANGUAGE SET LANGUAGE { [ Sets the language environment Not Not applicable
N ] 'language' | for the session and system applicable
@language_var } messages. SET LANGUAGE isn't
a date or time function.
However, the language setting
affects the output of date
functions.

sp_helplanguage sp_helplanguage [ [ Returns information about date Not Not applicable


@language = ] formats of all supported applicable
'language' ] languages. sp_helplanguage
isn't a date or time stored
procedure. However, the
language setting affects the
output of date functions.

Functions that validate date and time values

https://learn.microsoft.com/en-us/sql/t-sql/functions/date-and-time-data-types-and-functions-transact-sql?view=sql-server-ver16 7/8
2/5/23, 11:31 PM Date and Time Data Types and Functions - SQL Server (Transact-SQL) | Microsoft Learn

Function Syntax Return value Return Determinism


data
type

ISDATE ISDATE ( Determines whether a int ISDATE is deterministic only used with
expression ) datetime or smalldatetime the CONVERT function, when the
input expression has a valid CONVERT style parameter is specified,
date or time value. and when style isn't equal to 0, 100, 9,
or 109.

Date and time-related articles


Article Description

FORMAT Returns a value formatted with the specified format and optional culture. Use
the FORMAT function for locale-aware formatting of date/time and number
values as strings.

CAST and CONVERT Provides information about the conversion of date and time values to and from
(Transact-SQL) string literals, and other date and time formats.

Write International Provides guidelines for portability of databases and database applications that
Transact-SQL Statements use Transact-SQL statements from one language to another, or that support
multiple languages.

ODBC Scalar Functions Provides information about ODBC scalar functions available for use in Transact-
(Transact-SQL) SQL statements. This includes ODBC date and time functions.

AT TIME ZONE (Transact- Provides time zone conversion.


SQL)

See also
Functions
Data Types (Transact-SQL)

https://learn.microsoft.com/en-us/sql/t-sql/functions/date-and-time-data-types-and-functions-transact-sql?view=sql-server-ver16 8/8

You might also like