question

siera_gld avatar image
siera_gld asked

sql for day of the week in the future

I need to script and find the date for tuesday - or dw = 3 when today is thursday...or friday... but i need to use the tue date as a constant value to place in a report that will be used for an order date.
sqlt-sqldates
1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

ThomasRushton avatar image ThomasRushton ♦♦ commented ·
So are you wanting a query that will, in effect, return "Next Tuesday's date"?
0 Likes 0 ·
siera_gld avatar image
siera_gld answered
all of the time - so it should not matter if it's thur or friday at the time the query id executed, tue should be the return date.
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Squirrel avatar image
Squirrel answered
simplest way without complicated arithmetic operations is declare @dte date select @dte = '2011-10-17' select dateadd(day, case datename(weekday, @dte) when 'Sunday' then 2 when 'Monday' then 1 when 'Tuesday' then 7 when 'Wednesday' then 6 when 'Thursday' then 5 when 'Friday' then 4 when 'Saturday' then 3 end, @dte)
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Magnus Ahlkvist avatar image
Magnus Ahlkvist answered
SET DATEFIRST 1 DECLARE @dt DATE SET @dt = '2011-05-13' SELECT DATEADD(day, CASE WHEN datepart(weekday,@dt)<2 THEN 2-datepart(weekday,@dt) ELSE 9-datepart(weekday,@dt) END, @dt)
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

DannyC avatar image
DannyC answered
I know this is very late, but I was looking for the same answer today and found this: select getdate(),dateadd(week, datediff(week, 0, getdate()), 9) I am including GetDate() in the results only to prove that if GetDate() changes, the returned value will always be next Wednesday (in this example). Change the 9 to an 8 for Tuesday.
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.