.

Saturday, June 14, 2008

TIME DataType in SQL2008

TIME DataType in SQL2008

SQL Server 2008 introduces a TIME data type which allows us to store the time without the date.

For Example,

DECLARE @t TIME = '17:32:19'
SELECT [Time] = @t


Time
----------------
17:32:19.0000000

The TIME data type also allows you to define the accuracy. This indicates how many places to the right of the decimal are stored for the seconds portion.

DECLARE @t0 TIME(0) = '17:32:19.1234567',
@t7 TIME(7) = '17:32:19.1234567'
SELECT [Time0] = @t0, [Time7] = @t7


Time0 Time7
-------- ----------------
17:32:19 17:32:19.1234567

You can define from zero to seven places to the right of the decimal. A TIME(0) takes three bytes to store and a TIME(7) takes five bytes to store. If you declare a TIME variable without the accuracy it defaults to TIME(7).

TIME will do an implicit conversion from DATETIME and retain only the time portion. TIME will implicitly accept strings in most common time formats.

DECLARE @d1 DATETIME = '12/19/2007 13:43:23.45',
@t1 TIME(2)
SELECT @t1 = @d1
SELECT TimeOnly = @t1

No comments:

.