Thursday, March 12, 2009

TIME data type in SQL Server 2008

SQL Server 2008 has introduced the TIME data type to store Time without the Date. Before SQL Server 2008, we had to use the DATETIME data type to store Time only data, which was an absolute waste.

Usage of this data type is quiet straight forward.

DECLARE @tm TIME

SET @tm = '12:34:56.1234'

It accepts most common time formats in string format. Assigning a normal DATETIME data to TIME data type is also possible, in which case, only the Time part of the DATETIME data will be stored.

You can also provide an accuracy value for the data type while declaring:

DECLARE @tm TIME(5)

SET @tm = '12:34:56.1234567'

PRINT @tm

-- Output: 12:34:56.123457

The default accuracy is 7 digits. But depending on the application of the data type, you can even set it to 0.

Technorati Tags: ,
del.icio.us Tags: ,

No comments: