More on DATEs and SQL
Building on this First/Last day of month post (oddly, one of the main ways people seem to land on my blog), here are some other commonly needed SQL queries:
Beginning of period
Midnight of any day (i.e. truncate the time from a date)
SELECT DATEADD(dd, DATEDIFF(dd, 0, TheDate), 0)
This works by subtracting the supplied date (like GetDate() for today) from zero--which Microsoft SQL Server interprets as 1900-01-01 00:00:00 and gives the number of days. This value is then re-added to the zero date yielding the same date with the time truncated.
by
grcm
2007-02-14 04:49
SQL
·
SQL Server
·
datetime
·
date
·
time
·
timestamp