lawrencebenson
asked on
SQL Server: First Wednesday of the year
This one seems pretty easy...
I need to know how to pull the date for the first Wednesday of the year for the current year.
The SQL needs to be reusable (i.e. the report I am building will be run in 2012) so I can't have any dates hard coded.
Thanks!
I need to know how to pull the date for the first Wednesday of the year for the current year.
The SQL needs to be reusable (i.e. the report I am building will be run in 2012) so I can't have any dates hard coded.
Thanks!
--Find the previous wednesday (or today if it is a wednesday)
SET @Date = dateadd(d,(DATEPART(dw,@da te)*-1)+1, @Date)
-- Find how many weeks we are into year and subtract to get first wednesday
SET @Date = dateadd(ww,(datepart(ww,@D ate)*-1)+2 ,@Date)
--If you want to combine it in a single line
SELECT CAST(dateadd(ww,(datepart( ww,dateadd (d,(DATEPA RT(dw,getd ate())*-1) +1,getdate ()))*-1)+2 ,dateadd(d ,(DATEPART (dw,getdat e())*-1)+1 ,getdate() ))) as date
SET @Date = dateadd(d,(DATEPART(dw,@da
-- Find how many weeks we are into year and subtract to get first wednesday
SET @Date = dateadd(ww,(datepart(ww,@D
--If you want to combine it in a single line
SELECT CAST(dateadd(ww,(datepart(
SET DATEFIRST 3
DECLARE @Date date = '11/9/12'
SET @Date = DATEADD(yy, -1, @Date)
--Find the previous wednesday (or today if it is a wednesday)
SELECT dateadD(d,8-DATEPART(dw,@d ate),@Date )
--Or on one line
SELECT cast(dateadd(d,8-DATEPART( dw,DATEADD (yy, -1, getdate()) ),DATEADD(yy, -1, getdate()) ) as date)
If the day one year ago is a wednesday, it will return the following wednesday. If you want it to return that day, then test for it.
DECLARE @Date date = '11/9/12'
SET @Date = DATEADD(yy, -1, @Date)
--Find the previous wednesday (or today if it is a wednesday)
SELECT dateadD(d,8-DATEPART(dw,@d
--Or on one line
SELECT cast(dateadd(d,8-DATEPART(
If the day one year ago is a wednesday, it will return the following wednesday. If you want it to return that day, then test for it.
SELECT DATEADD(DAY, DATEDIFF(DAY, '19000103', CAST(YEAR(GETDATE() - 1) AS char(4)) + '0107') / 7 * 7, '19000103')
CORRECTION:
...
CAST(YEAR(GETDATE()) - 1 AS char(4))
...
...
CAST(YEAR(GETDATE()) - 1 AS char(4))
...
That code will give you the first Wednesday of any year.
GETDATE() = current year
GETDATE() - 1 yr = previous year
GETDATE() + 2 yrs = two years from now
etc.
GETDATE() = current year
GETDATE() - 1 yr = previous year
GETDATE() + 2 yrs = two years from now
etc.
ASKER
Scott, you hurt my brain. Seems to work, what did you do there?
ASKER
Scott, I actually need this to display the last twelve months / year of data. i.e. starting from nov 2010.
>> I need to know what the date of the first Wednesday after DATEADD(yy, -1, getdate()) is. <<
Sorry, didn't adjust my code to match that change.
SELECT DATEADD(DAY, DATEDIFF(DAY, '19000103', CONVERT(CHAR(8), DATEADD(YEAR, -1, GETDATE()), 112)) / 7 * 7, '19000103')
That code will find you the first Wednesday on or before one year back from the current date, inclusive. That is, on Nov 10 (which was a Wed in 2010), the code will return Nov 10, not Nov 3.
If you need to go back to the prev Wed in that case, subtract 1 day from GETDATE().
The code basically just gives you the first Wed on or before the date you put in the calc.
It works by starting with a base Wed date -- 19000103 -- and calc'ing the number of elapsed days since that date. It then divides that by 7 -- which is int arith so any fraction will drop -- then multiplies by 7. So a whole number of weeks is always added back to the Wed base date, always yielding a Wed :-) .
Jan 1900 is a good base date for working with days because the 01 Jan 1900 = Mon, 02 Jan = Tues, etc.. So it's pretty easy to remember the base dates for each day.
Sorry, didn't adjust my code to match that change.
SELECT DATEADD(DAY, DATEDIFF(DAY, '19000103', CONVERT(CHAR(8), DATEADD(YEAR, -1, GETDATE()), 112)) / 7 * 7, '19000103')
That code will find you the first Wednesday on or before one year back from the current date, inclusive. That is, on Nov 10 (which was a Wed in 2010), the code will return Nov 10, not Nov 3.
If you need to go back to the prev Wed in that case, subtract 1 day from GETDATE().
The code basically just gives you the first Wed on or before the date you put in the calc.
It works by starting with a base Wed date -- 19000103 -- and calc'ing the number of elapsed days since that date. It then divides that by 7 -- which is int arith so any fraction will drop -- then multiplies by 7. So a whole number of weeks is always added back to the Wed base date, always yielding a Wed :-) .
Jan 1900 is a good base date for working with days because the 01 Jan 1900 = Mon, 02 Jan = Tues, etc.. So it's pretty easy to remember the base dates for each day.
ASKER
This is a great solution, just one thing is a little bit off. One year ago from today is 2010-11-08, a Monday. I need to know the first Wednesday after that which is 2010-11-10. Your solution is returning 2010-11-03.
They reason for this is that I do not want to capture any partial work weeks.
They reason for this is that I do not want to capture any partial work weeks.
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
I did not end up going with this solution because I could not make it work in my solution however my testing indicated that it did work.
ASKER
I need to know what the date of the first Wednesday after DATEADD(yy, -1, getdate()) is.