- Article
Applies to: SQL Server
Azure SQL Database
Azure SQL Managed Instance
Azure Synapse Analytics
Analytics Platform System (PDW)
SQL analytics endpoint in Microsoft Fabric
Warehouse in Microsoft Fabric
Defines a date in SQL Server. The date data type was introduced in SQL Server 2008 (10.0.x).
date description
Property | Value |
---|---|
Syntax | DATE |
Usage | DECLARE @MyDate DATE
|
Default string literal format (used for down-level client) | yyyy-MM-dd For more information, see the Backward compatibility for down-level clients section. |
Range | 0001-01-01 through 9999-12-31 (1582-10-15 through 9999-12-31 for Informatica)January 1, 1 CE (Common Era) through December 31, 9999 CE (October 15, 1582 CE through December 31, 9999 CE for Informatica) |
Element ranges | yyyy is four digits from 0001 to 9999 that represent a year. Informatica limits yyyy to the range 1582 to 9999 .
|
Character length | 10 positions |
Precision, scale | 10, 0 |
Storage size | 3 bytes, fixed |
Storage structure | one 3-byte integer stores date |
Accuracy | One day |
Default value | 1900-01-01 This value is used for the appended date part for implicit conversion from time to datetime2 or datetimeoffset. |
Calendar | Gregorian |
User-defined fractional second precision | No |
Time zone offset aware and preservation | No |
Daylight saving aware | No |
Supported string literal formats for date
The following tables show the valid string literal formats for the date data type.
Numeric | Description |
---|---|
mdy
| [m]m , dd , and [yy]yy represent month, day, and year in a string with slash marks (/ ), hyphens (- ), or periods (. ) as separators.Only four-digit or two-digit years are supported. Use four-digit years whenever possible. To specify an integer from Note: For Informatica, A two-digit year that is less than or equal to the last two digits of the cutoff year is in the same century as the cutoff year. A two-digit year greater than the last two digits of the cutoff year is in the century that comes before the cutoff year. For example, if the two-digit year cutoff is the default The current language setting determines the default date format. You can change the date format by using the SET LANGUAGE and SET DATEFORMAT statements. The |
Alphabetical | Description |
---|---|
mon [dd][,] yyyy
| mon represents the full month name, or the month abbreviation, given in the current language. Commas are optional and capitalization is ignored.To avoid ambiguity, use four-digit years. If the day is missing, the first day of the month is supplied. |
ISO 8601 | Description |
---|---|
yyyy-MM-dd
| Same as the SQL standard. This format is the only format defined as an international standard. |
Unseparated | Description |
---|---|
[yy]yyMMdd
| The date data can be specified with four, six, or eight digits. A six-digit or eight-digit string is always interpreted as ymd . The month and day must always be two digits. A four-digit string is interpreted as the year. |
ODBC | Description |
---|---|
{ d 'yyyy-MM-dd' } | ODBC API specific. |
W3C XML format | Description |
---|---|
yyyy-MM-ddTZD | Supported for XML/SOAP usage.
- |
ANSI and ISO 8601 compliance
date complies with the ANSI SQL standard definition for the Gregorian calendar:
NOTE 85 - Datetime data types will allow dates in the Gregorian format to be stored in the date range 0001-01-01 CE through 9999-12-31 CE.
The default string literal format, which is used for down-level clients, complies with the SQL standard form that is defined as yyyy-MM-dd
. This format is the same as the ISO 8601 definition for DATE
.
Note
For Informatica, the range is limited to 1582-10-15
(October 15, 1582 CE) to 9999-12-31
(December 31, 9999 CE).
Backward compatibility for down-level clients
Some down-level clients don't support the time, date, datetime2, and datetimeoffset data types. The following table shows the type mapping between an up-level instance of SQL Server and down-level clients.
SQL Server data type | Default string literal format passed to down-level client | Down-level ODBC | Down-level OLEDB | Down-level JDBC | Down-level SQLCLIENT |
---|---|---|---|---|---|
time | hh:mm:ss[.nnnnnnn] | SQL_WVARCHAR or SQL_VARCHAR | DBTYPE_WSTR or DBTYPE_STR | Java.sql.String | String or SqString |
date | yyyy-MM-dd | SQL_WVARCHAR or SQL_VARCHAR | DBTYPE_WSTR or DBTYPE_STR | Java.sql.String | String or SqString |
datetime2 | yyyy-MM-dd HH:mm:ss[.nnnnnnn] | SQL_WVARCHAR or SQL_VARCHAR | DBTYPE_WSTR or DBTYPE_STR | Java.sql.String | String or SqString |
datetimeoffset | yyyy-MM-dd HH:mm:ss[.nnnnnnn] [+ or -]hh:mm | SQL_WVARCHAR or SQL_VARCHAR | DBTYPE_WSTR or DBTYPE_STR | Java.sql.String | String or SqString |
Convert date and time data
When you convert to date and time data types, SQL Server rejects all values it doesn't recognize as dates or times. For information about using the CAST
and CONVERT
functions with date and time data, see CAST and CONVERT (Transact-SQL).
Convert date to other date and time types
This section describes what occurs when you convert a date data type to other date and time data types.
When the conversion is to time(n), the conversion fails, and error message 206 is raised:
Operand type clash: date is incompatible with time.
If the conversion is to datetime, the date component is copied. The following code shows the results of converting a date value to a datetime value.
DECLARE @date DATE = '12-10-25';DECLARE @datetime DATETIME = @date;SELECT @date AS '@date', @datetime AS '@datetime';
Here is the result set.
@date @datetime---------- -----------------------2025-12-10 2025-12-10 00:00:00.000
When the conversion is to smalldatetime, the date value is in the range of a smalldatetime, the date component is copied, and the time component is set to 00:00:00.000
. When the date value is outside the range of a smalldatetime value, error message 242 is raised, and the smalldatetime value is set to NULL
:
The conversion of a date data type to a smalldatetime data types resulted in an out-of-range value.
The following code shows the results of converting a date value to a smalldatetime value.
DECLARE @date DATE = '1912-10-25';DECLARE @smalldatetime SMALLDATETIME = @date;SELECT @date AS '@date', @smalldatetime AS '@smalldatetime';
Here is the result set.
@date @smalldatetime---------- -------------------1912-10-25 1912-10-25 00:00:00
For conversion to datetimeoffset(n), date is copied, and the time is set to 00:00.0000000 +00:00
. The following code shows the results of converting a date value to a datetimeoffset(3) value.
DECLARE @date DATE = '1912-10-25';DECLARE @datetimeoffset DATETIMEOFFSET(3) = @date;SELECT @date AS '@date', @datetimeoffset AS '@datetimeoffset';
Here is the result set.
@date @datetimeoffset---------- ------------------------------1912-10-25 1912-10-25 00:00:00.000 +00:00
When the conversion is to datetime2(n), the date component is copied, and the time component is set to 00:00.000000
. The following code shows the results of converting a date value to a datetime2(3) value.
DECLARE @date DATE = '1912-10-25'DECLARE @datetime2 DATETIME2(3) = @date;SELECT @date AS '@date', @datetime2 AS '@datetime2(3)';
Here is the result set.
@date @datetime2(3)---------- -----------------------1912-10-25 1912-10-25 00:00:00.000
Convert string literals to date
Conversions from string literals to date and time types are allowed if all parts of the strings are in valid formats. Otherwise, a runtime error is raised. Implicit conversions or explicit conversions that don't specify a style, from date and time types to string literals, are in the default format of the current session. The following table shows the rules for converting a string literal to the date data type.
Input string literal | date |
---|---|
ODBC DATE | ODBC string literals are mapped to the datetime data type. Any assignment operation from ODBC DATETIME literals into a date type causes an implicit conversion between datetime and the type that the conversion rules define. |
ODBC TIME | See previous ODBC DATE rule. |
ODBC DATETIME | See previous ODBC DATE rule. |
DATE only | Trivial |
TIME only | Default values are supplied. |
TIMEZONE only | Default values are supplied. |
DATE + TIME | The DATE part of the input string is used. |
DATE + TIMEZONE | Not allowed. |
TIME + TIMEZONE | Default values are supplied. |
DATE + TIME + TIMEZONE | The DATE part of local DATETIME is used. |
Examples
The following example compares the results of casting a string to each date and time data type.
SELECT CAST('2022-05-08 12:35:29.1234567 +12:15' AS TIME(7)) AS 'time', CAST('2022-05-08 12:35:29.1234567 +12:15' AS DATE) AS 'date', CAST('2022-05-08 12:35:29.123' AS SMALLDATETIME) AS 'smalldatetime', CAST('2022-05-08 12:35:29.123' AS DATETIME) AS 'datetime', CAST('2022-05-08 12:35:29.1234567 +12:15' AS DATETIME2(7)) AS 'datetime2', CAST('2022-05-08 12:35:29.1234567 +12:15' AS DATETIMEOFFSET(7)) AS 'datetimeoffset';
Here is the result set.
Data type | Output |
---|---|
time | 12:35:29.1234567 |
date | 2022-05-08 |
smalldatetime | 2022-05-08 12:35:00 |
datetime | 2022-05-08 12:35:29.123 |
datetime2 | 2022-05-08 12:35:29.1234567 |
datetimeoffset | 2022-05-08 12:35:29.1234567 +12:15 |
See also
- CAST and CONVERT (Transact-SQL)
SQL Server is a robust relational database management system developed by Microsoft. It includes various data types to manage and manipulate different kinds of data. The date data type, introduced in SQL Server 2008, offers specific properties, syntax, and functionalities tailored for handling date-related information within the database.
The date data type within SQL Server facilitates the storage and manipulation of date values. It's defined with specific characteristics, including:
Properties:
- Syntax:
DATE
- Usage:
- Declaration:
DECLARE @MyDate DATE
- Table Creation:
CREATE TABLE Table1 (Column1 DATE)
- Declaration:
- Default String Literal Format (for down-level clients):
yyyy-MM-dd
- Range: From January 1, 1 CE (Common Era) through December 31, 9999 CE (1582-10-15 through 9999-12-31 for Informatica)
- Element Ranges: yyyy (year), MM (month), dd (day)
- Character Length: 10 positions
- Precision, Scale: 10, 0
- Storage Size: 3 bytes (fixed)
- Storage Structure: One 3-byte integer stores the date
- Accuracy: Accurate to a day
- Default Value: 1900-01-01
Supported String Literal Formats for Date:
SQL Server supports various string literal formats for dates, allowing flexibility in how dates can be represented and interpreted. Some of these formats include:
- Numeric descriptions like
yyyymmdd
ormm/dd/yyyy
- Alphabetic descriptions such as
mon yyyy
oryyyy mon
- ISO 8601 format (
yyyy-MM-dd
oryyyyMMdd
) - Unseparated formats (
yyMMdd
oryyyyMMdd
) - ODBC and W3C XML formats
Conversion and Compatibility:
SQL Server provides conversion options between date types and other related data types, like datetime
, smalldatetime
, datetime2
, and datetimeoffset
. It also covers conversions from string literals to date types and vice versa, ensuring compatibility and data integrity within the database.
Additionally, SQL Server supports setting the date format based on the current language settings, allowing users to adjust how dates are displayed and interpreted within the system.
The article you provided seems to comprehensively cover the date data type in SQL Server, detailing its syntax, supported formats, conversion rules, and compatibility considerations. If you have any specific queries or need further details on a particular aspect, feel free to ask!