
Using Date and Time Formats
String literal formats affect the presentation of data in applications to users but not the underlying integer storage format in SQL Server. However, SQL Server might interpret a date value in a string literal format, input by an application or user for storage or to a date function, as different dates. The interpretation depends on the combination of string literal format, data type, and runtime SET DATEFORMAT, SET LANGUAGE and default language option settings.
Some string literal formats are not affected by these settings. Consider using a format that does not depend on these settings, unless you know the settings are correct for the format. The ISO 8601 format does not depend on these settings and is an international standard. Transact-SQL that uses string literal formats, dependent on system settings, is less portable.
To find out the default string literal format for down-level clients, see the topic for each date and time data type. For an overview of all Transact-SQL date and time data types and functions, see Date and Time Data Types and Functions (Transact-SQL).
The ydm date format is not supported for the date, datetime2 and datetimeoffset types. A run time error will be raised.
String Literal Date and Time Formats
The following table lists different date and time string formats. We recommend that you use date-time formats that are not DATEFORMAT dependent and are multilanguage. The ISO 8601 formats, '1998-02-23T14:23:05' and '1998-02-23T14:23:05-08:00' are the only formats that are an international standard. They are not DATEFORMAT or default login language dependent and are multilanguage.
|
Date-time part
|
Format type
|
Format example
|
Can combine with other formats
|
DATEFORMAT dependent
|
Multilanguage
|
|---|
|
Date
|
Un-separated
ISO 8601
|
'19980223'
|
Yes
|
No
|
Yes
|
|
Date
|
Numeric
|
'02/23/1998'
|
Yes
|
yes
|
No
(DATEFORMAT)
|
|
Date
|
ISO 8601 Numeric
|
'1998-02-23'
|
Yes
|
No
|
No
|
|
Date
|
Alphabetical
|
'23 February 1998'
|
Yes
|
No
|
No
(month or short month)
|
|
Date
|
ODBC date
|
{d '1998-02-23'}
|
No
|
No
|
Yes
|
|
Time
|
ISO 8601 Time
|
'14:23:05'
'10:00:00.123456'
|
Yes
|
No
|
Yes
|
|
Time
|
ODBC time
|
{t '14:23:05'}
|
No
|
No
|
Yes
|
|
Date-time
|
ODBC date-time
|
{ts '1998-02-23 14:23:05'}
|
No
|
No
|
Yes
|
|
Date-time
|
ISO 8601
|
'1998-02-23T14:23:05'
'1998-02-23T14:23:05 -08:00'
|
No
|
No
|
Yes date, datetime2, datetimeoffset.
|
|
Date-time
|
ANSI SQL Standard
|
'1998-02-23 14:23:05'
'1998-02-23 14:23:05 -08:00'
|
No
|
No (datetime2, datetimeoffset)
Yes (datetime)
|
Yes date, datetime2, datetimeoffset.
|
|
Date-time
|
Combination of date and time
|
'19980223 14:23:05'
'02/23/1998 2:23:05 PM'
'1998-02-23 10:00:00.123'
'23 Feb 1998 14:23:05'
|
No
|
Yes
(date part)
|
No
|
|
TimeZone
|
TimeZone Format
|
'+12:00'
'01:00'
'-08:00'
'Z'
|
Yes
|
No
|
Yes
|
The following statements show the effects of SET LANGUAGE and SET DATEFORMAT settings.
DECLARE @Today date = '12/1/2003';
DECLARE @MyTime time = '12:30:22';
DECLARE @MyDatetimeoffset datetimeoffset = '12/1/2003 12:30:22 -5:00';
SET LANGUAGE Italian
-- Returns: Changed language setting to Italiano.
SELECT DATENAME(month, @Today) AS 'Month Name';
-- Returns: dicembre
SELECT CONVERT(VARCHAR(30), @MyDatetimeoffset) AS 'DATETIMEOFFSET';
-- Returns: dic 1 2003 12:30PM -05:00
SELECT DATENAME(hour, @MyTime) As 'Hour Name';
-- Returns: 12
SET LANGUAGE us_english;
-- Returns: Changed language setting to us_english.
SELECT DATENAME(month, @Today) AS 'Month Name';
-- Returns: December
SELECT CONVERT(VARCHAR(30), @MyDatetimeoffset) AS 'DATETIMEOFFSET';
-- Returns: Dec 1 2003 12:30PM -05:00
SELECT DATENAME(hour, @MyTime) As 'Hour Name';
-- Returns: 12
GO
-- Set date format to month, day, year.
SET DATEFORMAT mdy;
GO
DECLARE @datevar date = '12/31/1998';
SELECT @datevar AS DateVar;
-- Returns: 1998-12-31
GO
-- Set date format to year, day, month.
SET DATEFORMAT ydm;
GO
DECLARE @datevar datetimeoffset = '1998/31/12 12:30:22 -05:00';
SELECT @datevar AS DateVar;
-- Returns: Msg 241, Conversion failed when converting
-- date and/or time from character string.
GO
-- Set date format to year, month, day.
SET DATEFORMAT ymd;
GO
DECLARE @datevar date = '12/31/1998';
SELECT @datevar AS DateVar;
-- Returns: 1998-12-31
GO
Unseparated String Format
You can specify date data as an unseparated string. The date data can be specified by using four, six, or eight digits, an empty string, or a time value without a date value.
The SET DATEFORMAT session setting does not apply to all-numeric date entries, such as numeric entries without separators. The six-digit or eight-digit strings are always interpreted as ymd. The month and day must always be two digits.
This is the valid un-separated string format: [19]960415
A string of only four digits is interpreted as the year. The month and date are set to January 1. When you specify only four digits, you must include the century.
ISO 8601 Format
The ISO 8601 date with time format is as follows:
-
YYYY-MM-DDThh:mm:ss[.nnnnnnn][{+|-}hh:mm]
-
YYYY-MM-DDThh:mm:ss[.nnnnnnn]Z (UTC, Coordinated Universal Time)
The T indicates the start of the time part of the date-time value.
To use the ISO 8601 format, you must specify each element in the format. This includes the T, the colons (:), the + or - , and the periods (.). The brackets indicate that the fractional seconds or time zone offset components are optional.
The time component is specified in the 24-hour format.
The advantage in using the ISO 8601 format is that it is an international standard. Date and time values that are specified by using this format are unambiguous. This format is not affected by the SET DATEFORMAT, SET LANGUAGE, of login default language settings.
The following are two examples of date-time values that are specified in the ISO 8601 format:
-
2004-05-23T14:25:10
-
2004-05-23T14:25:10.1234567+07:00
Alphabetical Date Formats
You can specify a month as a name, for example, April or the abbreviation Apr in English. These should be specified in the LANGUAGE setting of the session, for example, avril or avr in French. Commas are optional and capitalization is ignored.
Here are some guidelines for using alphabetical date formats:
-
Enclose the date and time data in single quotation marks (').
-
If you specify only the last two digits of the year, values less than the last two digits of the value of the two digit year cutoff configuration option are in the same century as the cutoff year. Values that are greater than or equal to the value of this option are in the century that comes before the cutoff year. For example, if two digit year cutoff is 2050 (default), 25 is interpreted as 2025 and 50 is interpreted as 1950. To avoid ambiguity, use four-digit years.
-
If the day is missing, the first day of the month is supplied.
-
The SET DATEFORMAT session setting is not applied when you specify the month in alphabetical form.
-
The following formats are the valid alphabetical formats for SQL Server date data. Characters that are enclosed in brackets are optional.
-
Apr[il] [15][,] 1996
-
Apr[il] 15[,] [19]96
-
Apr[il] 1996 [15]
-
-
[15] Apr[il][,] 1996
-
15 Apr[il][,][19]96
-
15 [19]96 apr[il]
-
[15] 1996 apr[il]
-
-
1996 APR[IL] [15]
-
1996 [15] APR[IL]
Numeric Date Formats
You can specify date data with a numeric month. For example, 5/20/97 represents the twentieth day of May 1997. When you use a numeric date format, specify the year, month, and day in a string with slash marks (/), hyphens (-), or periods (.) as separators. This string must appear in the following form:
number separator number separator number [time] [time]
The following numeric formats are valid:
-
[0]4/15/[19]96 -- (mdy)
-
[0]4-15-[19]96 -- (mdy)
-
[0]4.15.[19]96 -- (mdy)
-
[0]4/[19]96/15 -- (myd)
-
-
15/[0]4/[19]96 -- (dmy)
-
15/[19]96/[0]4 -- (dym)
-
[19]96/15/[0]4 -- (ydm)
-
[19]96/[0]4/15 -- (ymd)
The default language DATEFORMAT for a session is set by the default language for the login, a SET LANGUAGE statement, or a SET DATEFORMAT statement. When the language is set to us_english by the default login or the SET LANGUAGE statement, the default order for the date is mdy.
You can change the date order by using the SET DATEFORMAT statement. The setting for SET DATEFORMAT determines how date values are interpreted. If the order does not match the setting, the values are not interpreted as dates, because they are out of range or the values are misinterpreted. For example, 12/10/08 can be interpreted as one of six dates, depending on the DATEFORMAT setting. A four-digit year will be interpreted as year.
Time Formats
SQL Server recognizes the following formats for time data. Enclose each format with single quotation marks (').
-
14:30
-
14:30[:20:999]
-
14:30[:20.9]
-
4am
-
4 PM
-
[0]4[:30:20:500]AM
The following statements show the return values of different inputs to the CAST function.
SELECT CAST('01/01/2000 14:30' AS datetime2)
--Returns: 2000-01-01 14:30:00.0000000
SELECT CAST('01/01/2000 14:30:20:999' AS datetime2)
-- Returns: 2000-01-01 14:30:20.9990000
SELECT CAST('01/01/2000 14:30:20.9' AS datetime2)
-- Returns: 2000-01-01 14:30:20.9000000
SELECT CAST('01/01/2000 4am' AS datetime2)
-- Returns: 2000-01-01 04:00:00.0000000
SELECT CAST('01/01/2000 4 PM' AS datetime2)
-- Returns: 2000-01-01 16:00:00.0000000
SELECT CAST('01/01/2000 04:30:20:500AM' AS datetime2)
-- Returns: 2000-01-01 04:30:20.5000000
SELECT CAST('01/01/2000 04:30:20:500 AM' AS datetime2)
-- Returns: 2000-01-01 04:30:20.5000000
GO
You can specify a suffix of AM or PM to indicate if the time value is before or after 12 noon. The case of AM or PM is ignored.
Hours can be specified by using either a 12-hour or 24-hour clock. The hour values are interpreted as follows.
-
The hour value of 00 represents the hour after midnight (AM), regardless of whether you specify AM. You cannot specify PM when the hour equals 00.
-
Hour values from 01 through 11 represent the hours before noon if neither AM nor PM is specified. They also represent the hours before noon when AM is specified. They represent hours after noon if PM is specified.
-
The hour value 12 represents the hour that starts at noon if neither AM nor PM is specified. If AM is specified, it represents the hour that starts at midnight. If PM is specified, it represents the hour that starts at noon. For example: 12:01 is 1 minute after noon, as is 12:01 PM, while 12:01 AM is 1 minute after midnight. Specifying 12:01 AM is the same as specifying 00:01 or 00:01 AM.
-
Hour values from 13 through 23 represents hours after noon if AM or PM is not specified. They also represent the hours after noon when PM is specified. You cannot specify AM when the hour value is from 13 through 23.
-
An hour value of 24 is not valid; use 12:00 AM or 00:00 to represent midnight.
Milliseconds can be preceded by either a colon (:) or a period (.). If preceded by a colon, the number means thousandths-of-a-second. If preceded by a period, a single digit means tenths-of-a-second, two digits mean hundredths-of-a-second, and three digits mean thousandths-of-a-second. For example, 12:30:20:1 indicates twenty and one-thousandth seconds past 12:30; 12:30:20.1 indicates twenty and one-tenth seconds past 12:30.
ODBC Date-time Format
The ODBC API defines escape sequences to represent date and time values that ODBC calls timestamp data. This ODBC timestamp format is also supported by the OLE DB language definition (DBGUID-SQL) and the Microsoft OLE DB provider for SQL Server. Applications that use the ADO, OLE DB, and ODBC-based APIs can use this ODBC timestamp format to represent dates and times.
SQL Server always treats ODBC data as being of the datetime data type.
ODBC timestamp escape sequences are of the format:
{ literal_type 'constant_value' }
-
literal_type
-
Specifies the type of the escape sequence. The following are the valid arguments for literal_type.
d = date only
t = time only
ts = timestamp (time + date)
-
'
constant_value
'
-
Is the value of the escape sequence. constant_value must follow these formats for each literal_type.
|
literal_type
|
constant_value format
|
|---|
|
d
|
YYYY-MM-DD
|
|
t
|
hh:mm:ss[.fff]
|
|
ts
|
YYYY-MM-DD
hh:mm:ss[.fff]
|
TExamples of ODBC time and date constants are as follows:
-
{ ts '1998-05-02 01:23:56.123' }
-
{ d '1990-10-02' }
-
{ t '13:33:41' }
Do not confuse the ODBC and OLE DB timestamp data type name with the Transact-SQL timestamp data type name. The ODBC and OLE DB timestamp data type records dates and times. The Transact-SQL timestamp data type is a binary data type that has no time-related values.