Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
gammax500
Starting Member
9 Posts |
Posted - 2003-04-18 : 20:29:45
|
| Given a date @startingdate:How do I get the first monday EQUAL TO or following @startingdate,the first tuesday equal to or following @startingdate, etc?I know I can get the day of the week to which @startingdate belongs like:declare @thisday datetimeset @thisday = {d '2000-01-12'}SELECT dateadd(dd, 1 - datepart(dw, @thisday), @thisday) AS SundayDate,dateadd(dd, 2 - datepart(dw, @thisday), @thisday) AS MondayDate,dateadd(dd, 3 - datepart(dw, @thisday), @thisday) AS TuesdayDate,dateadd(dd, 4 - datepart(dw, @thisday),@thisday) AS WednesdayDate...But if @thisday is a Wednesday, then I would like the sunday, monday, tuesday following @thisday, and the others to stay what is returned above.Is this hard?TIA,AnnEdited by - gammax500 on 04/18/2003 20:34:43Edited by - gammax500 on 04/18/2003 20:57:24 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2003-04-18 : 20:54:23
|
Well this works, but hopefully someone else has a better solution. This solution gives you the first Monday equal to or following the current date.DECLARE @weekdate INTDECLARE @startingdate DATETIMESET @startingdate = GETDATE()SELECT @weekday = DATEPART(WEEKDAY, @startingdate)SELECT CASE WHEN @weekday = 1 THEN DATEADD(DAY, 1, @startingdate) WHEN @weekday = 2 THEN DATEADD(DAY, 7, @startingdate) WHEN @weekday = 3 THEN DATEADD(DAY, 6, @startingdate) WHEN @weekday = 4 THEN DATEADD(DAY, 5, @startingdate) WHEN @weekday = 5 THEN DATEADD(DAY, 4, @startingdate) WHEN @weekday = 6 THEN DATEADD(DAY, 3, @startingdate) WHEN @weekday = 7 THEN DATEADD(DAY, 2, @startingdate) END TaraEdited by - tduggan on 04/18/2003 20:55:35 |
 |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2003-04-18 : 20:59:43
|
| use dateadd and datepart(dwSomething likedeclare @i intselect @i = @@datefirstset datefirst 2select dateadd(dd,7 - datepart(dw,getdate()),getdate())set datefirst @i==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2003-04-18 : 21:10:38
|
| if you are sure about the datefirst on your systemsdateadd(dd,(9-datepart(dw,@d))%7,@d)will do it change the 9 depending on the day you want.==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
ValterBorges
Master Smack Fu Yak Hacker
1429 Posts |
Posted - 2003-04-18 : 21:16:12
|
| DECLARE @Monday AS SMALLINTDECLARE @Tuesday AS SMALLINTDECLARE @Wednesday AS SMALLINTDECLARE @Thursday AS SMALLINTDECLARE @Friday As SMALLINTDECLARE @Saturday AS SMALLINTDECLARE @Sunday AS SMALLINTDECLARE @StartingDate AS DATETIMEDECLARE @NextDay AS SMALLINTDECLARE @i AS SMALLINTSET @Monday = 2SET @Tuesday = 3SET @Wednesday = 4SET @Thursday = 5SET @Friday = 6SET @Saturday = 7SET @Sunday = 1SET @StartingDate = '20030421'SELECT @i = @@datefirst SET DATEFIRST @MondaySELECT @StartingDate + (7-datepart(dw,@StartingDate))SET DATEFIRST @imight as well post after all this |
 |
|
|
ValterBorges
Master Smack Fu Yak Hacker
1429 Posts |
Posted - 2003-04-18 : 21:17:27
|
| nice one NRSo simple it hurtsEdited by - ValterBorges on 04/18/2003 21:25:41 |
 |
|
|
gammax500
Starting Member
9 Posts |
Posted - 2003-04-18 : 21:19:54
|
| Thank you ALL for your very informative posts. I especially enjoyed the post by nr. Thanks again,Ann |
 |
|
|
|
|
|
|
|