SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


How to find the 2nd Monday following a given date.


How to find the 2nd Monday following a given date.

Author
Message
fstop
fstop
Old Hand
Old Hand (304 reputation)Old Hand (304 reputation)Old Hand (304 reputation)Old Hand (304 reputation)Old Hand (304 reputation)Old Hand (304 reputation)Old Hand (304 reputation)Old Hand (304 reputation)

Group: General Forum Members
Points: 304 Visits: 316
Can someone help me figure this problem out? Thanks
ck9663
ck9663
SSC Veteran
SSC Veteran (215 reputation)SSC Veteran (215 reputation)SSC Veteran (215 reputation)SSC Veteran (215 reputation)SSC Veteran (215 reputation)SSC Veteran (215 reputation)SSC Veteran (215 reputation)SSC Veteran (215 reputation)

Group: General Forum Members
Points: 215 Visits: 111
How about this?


set datefirst 1

declare @basedate as datetime

set @basedate = '20130808'

select
Today = @basedate,
NextMonday = dateadd(dd, 8-datepart(dw, @basedate), @basedate),
MondayAfterNext = dateadd(dd, 15-datepart(dw, @basedate), @basedate)



Happy Coding!!!


~~ CK
fstop
fstop
Old Hand
Old Hand (304 reputation)Old Hand (304 reputation)Old Hand (304 reputation)Old Hand (304 reputation)Old Hand (304 reputation)Old Hand (304 reputation)Old Hand (304 reputation)Old Hand (304 reputation)

Group: General Forum Members
Points: 304 Visits: 316
Thank you. That works beautifully. Can you tell me what "set datefirst 1" is used for? It doesn't change anything if I remove it.
ck9663
ck9663
SSC Veteran
SSC Veteran (215 reputation)SSC Veteran (215 reputation)SSC Veteran (215 reputation)SSC Veteran (215 reputation)SSC Veteran (215 reputation)SSC Veteran (215 reputation)SSC Veteran (215 reputation)SSC Veteran (215 reputation)

Group: General Forum Members
Points: 215 Visits: 111
To make sure first day of the week is Monday so you can assume that, to get the next Monday, just add 8 minus the current day of the week. Monday after next can be calculated by adding 15 minus current day of the week.

Here's the full description from BOL.
fstop
fstop
Old Hand
Old Hand (304 reputation)Old Hand (304 reputation)Old Hand (304 reputation)Old Hand (304 reputation)Old Hand (304 reputation)Old Hand (304 reputation)Old Hand (304 reputation)Old Hand (304 reputation)

Group: General Forum Members
Points: 304 Visits: 316
Thanks
SwePeso
SwePeso
SSCrazy Eights
SSCrazy Eights (9.8K reputation)SSCrazy Eights (9.8K reputation)SSCrazy Eights (9.8K reputation)SSCrazy Eights (9.8K reputation)SSCrazy Eights (9.8K reputation)SSCrazy Eights (9.8K reputation)SSCrazy Eights (9.8K reputation)SSCrazy Eights (9.8K reputation)

Group: General Forum Members
Points: 9775 Visits: 3433
Remember that SET DATEFIRST will recompile your code. And also, not everyone has DATEFIRST 1 as default setting.

Try this code that is safe and doesn't rely on any setting and also, if doesn't have the annoying time portion.


SELECT GETDATE() AS Today,
DATEADD(DAY, DATEDIFF(DAY, '19000101', GETDATE()) / 7 * 7, '19000108') AS NextMonday,
DATEADD(DAY, DATEDIFF(DAY, '19000101', GETDATE()) / 7 * 7, '19000115') AS MondayAfterNext




N 56°04'39.16"
E 12°55'05.25"
dwain.c
dwain.c
SSCoach
SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)

Group: General Forum Members
Points: 18145 Visits: 6431
SwePeso (8/11/2013)
Remember that SET DATEFIRST will recompile your code. And also, not everyone has DATEFIRST 1 as default setting.

Try this code that is safe and doesn't rely on any setting and also, if doesn't have the annoying time portion.


SELECT GETDATE() AS Today,
DATEADD(DAY, DATEDIFF(DAY, '19000101', GETDATE()) / 7 * 7, '19000108') AS NextMonday,
DATEADD(DAY, DATEDIFF(DAY, '19000101', GETDATE()) / 7 * 7, '19000115') AS MondayAfterNext



Isn't this more or less the same thing with a little less math?


SELECT Today=GETDATE()
,NextMonday=DATEADD(week, 1+DATEDIFF(week, 0, GETDATE()), 0)
,MondayAfterNext=DATEADD(week, 2+DATEDIFF(week, 0, GETDATE()), 0)





My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
SwePeso
SwePeso
SSCrazy Eights
SSCrazy Eights (9.8K reputation)SSCrazy Eights (9.8K reputation)SSCrazy Eights (9.8K reputation)SSCrazy Eights (9.8K reputation)SSCrazy Eights (9.8K reputation)SSCrazy Eights (9.8K reputation)SSCrazy Eights (9.8K reputation)SSCrazy Eights (9.8K reputation)

Group: General Forum Members
Points: 9775 Visits: 3433
Using WEEK as calculation is prone to at least two errors. In this case, if you are using a sunday you will get wrong result.
See this repro

DECLARE @Basedate DATETIME = '20130811';

SET DATEFIRST 1;

SELECT @@DATEFIRST AS [Current SET DATEFIRST setting],
DATEADD(week, 1+DATEDIFF(week, 0, @Basedate), 0) AS [NextMonday by Dwain.c],
DATEADD(week, 2+DATEDIFF(week, 0, @Basedate), 0) AS [MondayAfterNext by Dwain.c],
dateadd(dd, 8-datepart(dw, @Basedate), @Basedate) AS [NextMonday by ck9663],
dateadd(dd, 15-datepart(dw, @Basedate), @Basedate) AS [MondayAfterNext by ck9663],
DATEADD(DAY, DATEDIFF(DAY, '19000101', @Basedate) / 7 * 7, '19000108') AS [NextMonday by SwePeso],
DATEADD(DAY, DATEDIFF(DAY, '19000101', @Basedate) / 7 * 7, '19000115') AS [MondayAfterNext by SwePeso]

SET DATEFIRST 2;

SELECT @@DATEFIRST AS [Current SET DATEFIRST setting],
DATEADD(week, 1+DATEDIFF(week, 0, @Basedate), 0) AS [NextMonday by Dwain.c],
DATEADD(week, 2+DATEDIFF(week, 0, @Basedate), 0) AS [MondayAfterNext by Dwain.c],
dateadd(dd, 8-datepart(dw, @Basedate), @Basedate) AS [NextMonday by ck9663],
dateadd(dd, 15-datepart(dw, @Basedate), @Basedate) AS [MondayAfterNext by ck9663],
DATEADD(DAY, DATEDIFF(DAY, '19000101', @Basedate) / 7 * 7, '19000108') AS [NextMonday by SwePeso],
DATEADD(DAY, DATEDIFF(DAY, '19000101', @Basedate) / 7 * 7, '19000115') AS [MondayAfterNext by SwePeso]

SET DATEFIRST 3;

SELECT @@DATEFIRST AS [Current SET DATEFIRST setting],
DATEADD(week, 1+DATEDIFF(week, 0, @Basedate), 0) AS [NextMonday by Dwain.c],
DATEADD(week, 2+DATEDIFF(week, 0, @Basedate), 0) AS [MondayAfterNext by Dwain.c],
dateadd(dd, 8-datepart(dw, @Basedate), @Basedate) AS [NextMonday by ck9663],
dateadd(dd, 15-datepart(dw, @Basedate), @Basedate) AS [MondayAfterNext by ck9663],
DATEADD(DAY, DATEDIFF(DAY, '19000101', @Basedate) / 7 * 7, '19000108') AS [NextMonday by SwePeso],
DATEADD(DAY, DATEDIFF(DAY, '19000101', @Basedate) / 7 * 7, '19000115') AS [MondayAfterNext by SwePeso]

SET DATEFIRST 4;

SELECT @@DATEFIRST AS [Current SET DATEFIRST setting],
DATEADD(week, 1+DATEDIFF(week, 0, @Basedate), 0) AS [NextMonday by Dwain.c],
DATEADD(week, 2+DATEDIFF(week, 0, @Basedate), 0) AS [MondayAfterNext by Dwain.c],
dateadd(dd, 8-datepart(dw, @Basedate), @Basedate) AS [NextMonday by ck9663],
dateadd(dd, 15-datepart(dw, @Basedate), @Basedate) AS [MondayAfterNext by ck9663],
DATEADD(DAY, DATEDIFF(DAY, '19000101', @Basedate) / 7 * 7, '19000108') AS [NextMonday by SwePeso],
DATEADD(DAY, DATEDIFF(DAY, '19000101', @Basedate) / 7 * 7, '19000115') AS [MondayAfterNext by SwePeso]

SET DATEFIRST 5;

SELECT @@DATEFIRST AS [Current SET DATEFIRST setting],
DATEADD(week, 1+DATEDIFF(week, 0, @Basedate), 0) AS [NextMonday by Dwain.c],
DATEADD(week, 2+DATEDIFF(week, 0, @Basedate), 0) AS [MondayAfterNext by Dwain.c],
dateadd(dd, 8-datepart(dw, @Basedate), @Basedate) AS [NextMonday by ck9663],
dateadd(dd, 15-datepart(dw, @Basedate), @Basedate) AS [MondayAfterNext by ck9663],
DATEADD(DAY, DATEDIFF(DAY, '19000101', @Basedate) / 7 * 7, '19000108') AS [NextMonday by SwePeso],
DATEADD(DAY, DATEDIFF(DAY, '19000101', @Basedate) / 7 * 7, '19000115') AS [MondayAfterNext by SwePeso]

SET DATEFIRST 6;

SELECT @@DATEFIRST AS [Current SET DATEFIRST setting],
DATEADD(week, 1+DATEDIFF(week, 0, @Basedate), 0) AS [NextMonday by Dwain.c],
DATEADD(week, 2+DATEDIFF(week, 0, @Basedate), 0) AS [MondayAfterNext by Dwain.c],
dateadd(dd, 8-datepart(dw, @Basedate), @Basedate) AS [NextMonday by ck9663],
dateadd(dd, 15-datepart(dw, @Basedate), @Basedate) AS [MondayAfterNext by ck9663],
DATEADD(DAY, DATEDIFF(DAY, '19000101', @Basedate) / 7 * 7, '19000108') AS [NextMonday by SwePeso],
DATEADD(DAY, DATEDIFF(DAY, '19000101', @Basedate) / 7 * 7, '19000115') AS [MondayAfterNext by SwePeso]

SET DATEFIRST 7;

SELECT @@DATEFIRST AS [Current SET DATEFIRST setting],
DATEADD(week, 1+DATEDIFF(week, 0, @Basedate), 0) AS [NextMonday by Dwain.c],
DATEADD(week, 2+DATEDIFF(week, 0, @Basedate), 0) AS [MondayAfterNext by Dwain.c],
dateadd(dd, 8-datepart(dw, @Basedate), @Basedate) AS [NextMonday by ck9663],
dateadd(dd, 15-datepart(dw, @Basedate), @Basedate) AS [MondayAfterNext by ck9663],
DATEADD(DAY, DATEDIFF(DAY, '19000101', @Basedate) / 7 * 7, '19000108') AS [NextMonday by SwePeso],
DATEADD(DAY, DATEDIFF(DAY, '19000101', @Basedate) / 7 * 7, '19000115') AS [MondayAfterNext by SwePeso]




N 56°04'39.16"
E 12°55'05.25"
dwain.c
dwain.c
SSCoach
SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)

Group: General Forum Members
Points: 18145 Visits: 6431
Interesting results SWEPESO. Didn't think of checking Sunday.


My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
SwePeso
SwePeso
SSCrazy Eights
SSCrazy Eights (9.8K reputation)SSCrazy Eights (9.8K reputation)SSCrazy Eights (9.8K reputation)SSCrazy Eights (9.8K reputation)SSCrazy Eights (9.8K reputation)SSCrazy Eights (9.8K reputation)SSCrazy Eights (9.8K reputation)SSCrazy Eights (9.8K reputation)

Group: General Forum Members
Points: 9775 Visits: 3433
No problem.


N 56°04'39.16"
E 12°55'05.25"
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search