Date and Time Data Types and Functions (Transact-SQL)
The following sections in this topic provide an overview of all Transact-SQL date and time data types and functions. For information and examples that are common to date and time data types and functions, see Using Date and Time Data.
The Transact-SQL date and time data types are listed in the following table.
|
Data type |
Format |
Range |
Accuracy |
Storage size (bytes) |
User-defined fractional second precision |
Time zone offset |
|---|---|---|---|---|---|---|
|
hh:mm:ss[.nnnnnnn] |
00:00:00.0000000 through 23:59:59.9999999 |
100 nanoseconds |
3 to 5 |
Yes |
No |
|
|
YYYY-MM-DD |
0001-01-01 through 9999-12-31 |
1 day |
3 |
No |
No |
|
|
YYYY-MM-DD hh:mm:ss |
1900-01-01 through 2079-06-06 |
1 minute |
4 |
No |
No |
|
|
YYYY-MM-DD hh:mm:ss[.nnn] |
1753-01-01 through 9999-12-31 |
0.00333 second |
8 |
No |
No |
|
|
YYYY-MM-DD hh:mm:ss[.nnnnnnn] |
0001-01-01 00:00:00.0000000 through 9999-12-31 23:59:59.9999999 |
100 nanoseconds |
6 to 8 |
Yes |
No |
|
|
YYYY-MM-DD hh:mm:ss[.nnnnnnn] [+|-]hh:mm |
0001-01-01 00:00:00.0000000 through 9999-12-31 23:59:59.9999999 (in UTC) |
100 nanoseconds |
8 to 10 |
Yes |
Yes |
Note
|
|---|
|
The Transact-SQLrowversion data type is not a date or time data type. timestamp is a deprecated synonym for rowversion. |
The Transact-SQL date and time functions are listed in the following tables. For more information about determinism, see Deterministic and Nondeterministic Functions.
Functions That Get System Date and Time Values
All system date and time values are derived from the operating system of the computer on which the instance of SQL Server is running.
Higher-Precision System Date and Time Functions
SQL Server 2008 obtains the date and time values by using the GetSystemTimeAsFileTime() Windows API. The accuracy depends on the computer hardware and version of Windows on which the instance of SQL Server is running. The precision of this API is fixed at 100 nanoseconds. The accuracy can be determined by using the GetSystemTimeAdjustment() Windows API.
|
Function |
Syntax |
Return value |
Return data type |
Determinism |
|---|---|---|---|---|
|
SYSDATETIME () |
Returns a datetime2(7) value that contains the date and time of the computer on which the instance of SQL Server is running. The time zone offset is not included. |
datetime2(7) |
Nondeterministic |
|
|
SYSDATETIMEOFFSET ( ) |
Returns a datetimeoffset(7) value that contains the date and time of the computer on which the instance of SQL Server is running. The time zone offset is included. |
datetimeoffset(7) |
Nondeterministic |
|
|
SYSUTCDATETIME ( ) |
Returns a datetime2(7) value that contains the date and time of the computer on which the instance of SQL Server is running. The date and time is returned as UTC time (Coordinated Universal Time). |
datetime2(7) |
Nondeterministic |
Lower-Precision System Date and Time Functions
|
Function |
Syntax |
Return value |
Return data type |
Determinism |
|---|---|---|---|---|
|
CURRENT_TIMESTAMP |
Returns a datetime2(7) value that contains the date and time of the computer on which the instance of SQL Server is running. The time zone offset is not included. |
datetime |
Nondeterministic |
|
|
GETDATE ( ) |
Returns a datetime2(7) value that contains the date and time of the computer on which the instance of SQL Server is running. The time zone offset is not included. |
datetime |
Nondeterministic |
|
|
GETUTCDATE ( ) |
Returns a datetime2(7) value that contains the date and time of the computer on which the instance of SQL Server is running. The date and time is returned as UTC time (Coordinated Universal Time). |
datetime |
Nondeterministic |
Functions That Get Date and Time Parts
|
Function |
Syntax |
Return value |
Return data type |
Determinism |
|---|---|---|---|---|
|
DATENAME ( datepart , date ) |
Returns a character string that represents the specified datepart of the specified date. |
nvarchar |
Nondeterministic |
|
|
DATEPART ( datepart , date ) |
Returns an integer that represents the specified datepart of the specified date. |
int |
Nondeterministic |
|
|
DAY ( date ) |
Returns an integer that represents the day day part of the specified date. |
int |
Deterministic |
|
|
MONTH ( date ) |
Returns an integer that represents the month part of a specified date. |
int |
Deterministic |
|
|
YEAR ( date ) |
Returns an integer that represents the year part of a specified date. |
int |
Deterministic |
Functions That Get Date and Time Difference
|
Function |
Syntax |
Return value |
Return data type |
Determinism |
|---|---|---|---|---|
|
DATEDIFF ( datepart , startdate , enddate ) |
Returns the number of date or time datepart boundaries that are crossed between two specified dates. |
int |
Deterministic |
Functions That Modify Date and Time Values
|
Function |
Syntax |
Return value |
Return data type |
Determinism |
|---|---|---|---|---|
|
DATEADD (datepart , number , date ) |
Returns a new datetime value by adding an interval to the specified datepart of the specified date. |
The data type of the date argument |
Deterministic |
|
|
SWITCHOFFSET (DATETIMEOFFSET , time_zone) |
SWITCHOFFSET changes the time zone offset of a DATETIMEOFFSET value and preserves the UTC value. |
datetimeoffset with the fractional precision of the DATETIMEOFFSET |
Deterministic |
|
|
TODATETIMEOFFSET (expression , time_zone) |
TODATETIMEOFFSET transforms a datetime2 value into a datetimeoffset value. The datetime2 value is interpreted in local time for the specified time_zone. |
datetimeoffset with the fractional precision of the datetime argument |
Deterministic |
Functions That Set or Get Session Format
|
Function |
Syntax |
Return value |
Return data type |
Determinism |
|---|---|---|---|---|
|
@@DATEFIRST |
Returns the current value, for the session, of SET DATEFIRST. |
tinyint |
Nondeterministic |
|
|
SET DATEFIRST { number | @number_var } |
Sets the first day of the week to a number from 1 through 7. |
Not applicable |
Not applicable |
|
|
SET DATEFORMAT { format | @format_var } |
Sets the order of the dateparts (month/day/year) for entering datetime or smalldatetime data. |
Not applicable |
Not applicable |
|
|
@@LANGUAGE |
Returns the name of the language that is currently being used. @@LANGUAGE is not a date or time function. However, the language setting can affect the output of date functions. |
Not applicable |
Not applicable |
|
|
SET LANGUAGE { [ N ] 'language' | @language_var } |
Sets the language environment for the session and system messages. SET LANGUAGE is not a date or time function. However, the language setting affects the output of date functions. |
Not applicable |
Not applicable |
|
|
sp_helplanguage [ [ @language = ] 'language' ] |
Returns information about date formats of all supported languages. sp_helplanguage is not a date or time stored procedure. However, the language setting affects the output of date functions. |
Not applicable |
Not applicable |
Functions That Validate Date and Time Values
|
Function |
Syntax |
Return value |
Return data type |
Determinism |
|---|---|---|---|---|
|
ISDATE ( expression ) |
Determines whether a datetime or smalldatetime input expression is a valid date or time value. |
int |
ISDATE is deterministic only if you use it with the CONVERT function, when the CONVERT style parameter is specified, and when style is not equal to 0, 100, 9, or 109. |
|
Topic |
Description |
|---|---|
|
Provides information and examples that are common to date and time data types and functions. |
|
|
Provides information about the conversion of date and time values to and from string literals and other date and time formats. |
|
|
Provides guidelines for portability of databases and database applications that use Transact-SQL statements from one language to another, or that support multiple languages. |
|
|
Provides information about ODBC scalar functions that can be used in Transact-SQL statements. This includes ODBC date and time functions. |
|
|
Provides information about how date and time data types affect distributed queries between servers that have different versions of SQL Server or different providers. |
All three rows start with "Returns a datetime2(7)..." in the return value column, but show datetime in teh return datatype column. This is inconsistent. I would wager the old functions return datetime, not datetime2.
- 3/11/2010
- Wes Clark
- 11/18/2009
- Aaron Wang
- 11/19/2009
- Thomas Lee
CREATE FUNCTION fn_formatdatetime (@datetime datetime, @formatstr varchar(255))
RETURNS varchar(255)
AS
BEGIN
-- Author: Nickolas L. Hook
-- This Function works similar to the Format function in Visual Basic for creating Custom Formating Strings for Date/Time Variables
-- Valid characters for the @formatstr Are...
-- d Displays the day as a number without a leading zero (1 - 31)
-- dd Displays the day as a number with a leading zero (01 - 31)
-- ddd Displays the day as an abbreviation (Sun - Sat)
-- dddd Displays the day as a full name (Sunday - Saturday)
-- w Displays the day of the week as a number (1 for Sunday through 7 for Saturday)
-- m Displays the month as a number without a leading zero (1 - 12)
-- mm Displays the month as a number with a leading zero (01 - 12)
-- mmm Displays the month as an abbreviation (Jan - Dec)
-- mmmm Displays the month as a full month name (January - December)
-- yy Displays the year as a 2-digit number (00-99)
-- yyyy Displays the year as a 4-digit number (1000 - 9999)
-- q Displays the quarter of the year (1 - 4)
-- h Displays the hour as a number without leading zeros (0 - 23)
-- hh Displays the hour as a number with leading zeros (00 - 23)
-- th Displays the hour as a number without leading zeros (1 - 12)
-- n Displays the minute as a number without leading zeros (0 - 59)
-- nn Displays the minute as a number with leading zeros (00-59)
-- s Displays the second as a number without leading zeros (0 - 60)
-- ss Displays the second as a number with leading zeros (00 - 60)
-- am/pm Displays am before noon; Displays pm after noon through 11:59 P.M.
-- a/p Displays a before noon; Displays p after noon through 11:59 P.M.
-- Examples (assuming a date of March 7th, 2003 at 8:07:05 A.M.)
-- @formatstr Returns
-- m/d/yy 3/7/03
-- mmmm d, yyyy March 7, 2003
-- mm-dd-yyyy h:nnam/pm 03-07-2003 8:07am
DECLARE @outStr varchar(255)
DECLARE @datestr varchar(24)
DECLARE @meridian varchar(1)
DECLARE @temp varchar(2)
SET @outStr = @formatstr
SET @datestr = CONVERT(varchar(24), @datetime, 113)
-- dddd --
SET @outStr = REPLACE(@outStr, 'dddd',
CASE DATEPART(dw, @datetime)
WHEN 1 THEN 'Sunday'
WHEN 2 THEN 'Monday'
WHEN 3 THEN 'Tuesday'
WHEN 4 THEN 'Wednesday'
WHEN 5 THEN 'Thursday'
WHEN 6 THEN 'Friday'
WHEN 7 THEN 'Saturday'
END)
-- ddd --
SET @outStr = REPLACE(@outStr, 'ddd',
CASE DATEPART(dw, @datetime)
WHEN 1 THEN 'Sun'
WHEN 2 THEN 'Mon'
WHEN 3 THEN 'Tue'
WHEN 4 THEN 'Wed'
WHEN 5 THEN 'Thu'
WHEN 6 THEN 'Fri'
WHEN 7 THEN 'Sat'
END)
-- dd --
SET @outStr = REPLACE(@outStr, 'dd', SUBSTRING(@datestr,1,2))
-- d --
SET @outStr = REPLACE(@outStr, 'd', CONVERT(int,SUBSTRING(@datestr,1,2)))
-- w --
SET @outStr = REPLACE(@outStr, 'w', DATEPART(dw,@datetime))
-- yyyy --
SET @outStr = REPLACE(@outStr, 'yyyy', SUBSTRING(@datestr,8,4))
-- yy --
SET @outStr = REPLACE(@outStr, 'yy', SUBSTRING(@datestr,10,2))
-- q --
SET @outStr = REPLACE(@outStr, 'q', DATEPART(q,@datestr))
-- hh --
SET @outStr = REPLACE(@outStr, 'hh', SUBSTRING(@datestr,13,2))
-- th --
IF CONVERT(int,SUBSTRING(@datestr,13,2)) > 12
SET @outStr = REPLACE(@outStr, 'th', CONVERT(int,SUBSTRING(@datestr,13,2)) - 12)
ELSE SET @outStr = REPLACE(@outStr, 'th', SUBSTRING(@datestr,13,2))
-- h --
SET @outStr = REPLACE(@outStr, 'h', CONVERT(int,SUBSTRING(@datestr,13,2)))
-- nn --
SET @outStr = REPLACE(@outStr, 'nn', SUBSTRING(@datestr,16,2))
-- n --
SET @outStr = REPLACE(@outStr, 'n', CONVERT(int,SUBSTRING(@datestr,16,2)))
-- ss --
SET @outStr = REPLACE(@outStr, 'ss', SUBSTRING(@datestr,19,2))
-- s --
SET @outStr = REPLACE(@outStr, 's', CONVERT(int,SUBSTRING(@datestr,19,2)))
-- m, mm, mmm, mmmm (This is last because it put letters back into the @outStr and if done previously, things like the 'h' in 'March' become an hour --
IF CHARINDEX('m',@outStr,0) > 0 BEGIN
IF CHARINDEX('mm',@outStr,0) > 0 BEGIN
IF CHARINDEX('mmm',@outStr,0) > 0 BEGIN
IF CHARINDEX('mmmm',@outStr,0) > 0 BEGIN
SET @outStr = REPLACE(@outStr, 'mmmm',
CASE DATEPART(mm, @datetime)
WHEN 1 THEN 'January'
WHEN 2 THEN 'February'
WHEN 3 THEN 'March'
WHEN 4 THEN 'April'
WHEN 5 THEN 'May'
WHEN 6 THEN 'June'
WHEN 7 THEN 'July'
WHEN 8 THEN 'August'
WHEN 9 THEN 'September'
WHEN 10 THEN 'October'
WHEN 11 THEN 'November'
WHEN 12 THEN 'December'
END)
END
ELSE SET @outStr = REPLACE(@outStr, 'mmm', SUBSTRING(@datestr,4,3))
END
ELSE BEGIN
SET @temp = DATEPART(mm,@datetime)
IF (DATEPART(mm,@datetime)<10) SET @temp = '0' + @temp
SET @outStr = REPLACE(@outStr, 'mm', @temp)
END
END
ELSE BEGIN
SET @outStr = REPLACE(@outStr, 'm', DATEPART(mm,@datetime))
SET @outStr = REPLACE(@outStr, 'a'+CAST(DATEPART(mm,@datetime) AS varchar(1))+'/p'+CAST(DATEPART(mm,@datetime) AS varchar(1)),'am/pm')
END
END
-- Used by am/pm and a/p --
IF CONVERT(int,SUBSTRING(@datestr,13,2)) > 12
SET @meridian = 'p'
ELSE SET @meridian = 'a'
-- am/pm --
SET @outStr = REPLACE(@outStr, 'am/pm', @meridian+'m')
-- a/p --
SET @outStr = REPLACE(@outStr, 'a/p', @meridian)
RETURN @outStr
END
- 11/9/2009
- Manuel Alves
print replace(str(DatePart(hh, GetDate()), 2), ' ', '0') + ':' +
replace(str(DatePart(mi, GetDate()), 2), ' ', '0') + ':' +
replace(str(DatePart(s, GetDate()), 2), ' ', '0')
Prints the current time like:
15:04:20
- 4/9/2009
- hfrmobile
Note