Wednesday, July 14, 2010

SQL DateTime Queries

Here are some basic DateTime queries to get the date you need.



declare @BeginDate datetime;
set @BeginDate = getdate(); 
 
select dateadd(dd, datediff(dd, 0, @BeginDate), 0)     -- Beginning of this day
select dateadd(dd, datediff(dd, 0, @BeginDate) + 1, 0) -- Beginning of next day
select dateadd(dd, datediff(dd, 0, @BeginDate) - 1, 0) -- Beginning of previous day
select dateadd(wk, datediff(wk, 0, @BeginDate), 0)     -- Beginning of this week (Monday)
select dateadd(wk, datediff(wk, 0, @BeginDate) + 1, 0) -- Beginning of next week (Monday)
select dateadd(wk, datediff(wk, 0, @BeginDate) - 1, 0) -- Beginning of previous week (Monday)
select dateadd(mm, datediff(mm, 0, @BeginDate), 0)     -- Beginning of this month
select dateadd(mm, datediff(mm, 0, @BeginDate) + 1, 0) -- Beginning of next month
select dateadd(mm, datediff(mm, 0, @BeginDate) - 1, 0) -- Beginning of previous month
select dateadd(qq, datediff(qq, 0, @BeginDate), 0)     -- Beginning of this quarter (Calendar)
select dateadd(qq, datediff(qq, 0, @BeginDate) + 1, 0) -- Beginning of next quarter (Calendar)
select dateadd(qq, datediff(qq, 0, @BeginDate) - 1, 0) -- Beginning of previous quarter (Calendar)
select dateadd(yy, datediff(yy, 0, @BeginDate), 0)     -- Beginning of this year
select dateadd(yy, datediff(yy, 0, @BeginDate) + 1, 0) -- Beginning of next year
select dateadd(yy, datediff(yy, 0, @BeginDate) - 1, 0) -- Beginning of previous year