Tuesday, 6 October 2009

Adding time to dates in SQL Server

To add time to a date use the DATEADD Transact-SQL statement.

DATEADD (datepart , number, date )

The date parts can be:

datepart Abbreviations

year

yy, yyyy

quarter

qq, q

month

mm, m

dayofyear

dy, y

day

dd, d

week

wk, ww

weekday

dw, w

hour

hh

minute

mi, n

second

ss, s

millisecond

ms

microsecond

mcs

nanosecond

ns

For example:

SELECT DATEADD(dd, 12, "29 March 1964")

To get the date at midnight you can use the following:

SELECT DATEADD(dd,0, DATEDIFF(dd,0, GETDATE())) as date_at_midnight

See http://msdn.microsoft.com/en-us/library/ms186819.aspx for details.

0 comments:

Post a Comment

By all means leave a comment. I may not be able to get back to you as quickly as I'd like but I'll do my very best.