Wednesday, December 15, 2010

SQL 2008 DateTime dataTypes

SQL 2008 introduced new types of DateTime data type (Time, Date, DateTime, DateTime2, SmallDateTime and DateTimeOffset).
The following query example explains the difference between each one of them.
SELECT 
CAST('2007-05-08 12:35:29. 1234567 +12:15' AS time(7)) AS 'Time' 
,CAST('2007-05-08 12:35:29. 1234567 +12:15' AS date) AS 'Date' 
,CAST('2007-05-08 12:35:29.123' AS smalldatetime) AS 'Smalldatetime' 
,CAST('2007-05-08 12:35:29.123' AS datetime) AS 'Datetime' 
,CAST('2007-05-08 12:35:29.1234567+12:15' AS datetime2(7)) AS  'Datetime2'
,CAST('2007-05-08 12:35:29.1234567 +12:15' AS datetimeoffset(7)) AS   'Datetimeoffset'
,CAST('2007-05-08 12:35:29.1234567+12:15' AS datetimeoffset(7)) AS 'Datetimeoffset IS08601';

Here is the result set.


Data type

Output

Time

12:35:29. 1234567

Date

2007-05-08

Smalldatetime

2007-05-08 12:35:00

Datetime

2007-05-08 12:35:29.123

datetime2

2007-05-08 12:35:29. 1234567

Datetimeoffset

2007-05-08 12:35:29.1234567 +12:15



Reference:
MSDN

No comments:

Post a Comment