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


Finding the Correct Weekday Regardless of DateFirst


Finding the Correct Weekday Regardless of DateFirst

Author
Message
Divya Agrawal
Divya Agrawal
SSCommitted
SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)

Group: General Forum Members
Points: 1818 Visits: 607
Comments posted to this topic are about the item Finding the Correct Weekday Regardless of DateFirst

--Divya
Michael Meierruth
Michael Meierruth
SSCarpal Tunnel
SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)

Group: General Forum Members
Points: 4924 Visits: 2526
In many cases, I can do what I have to do by simply capturing the weekday values for a known saturday and sunday, like so:


declare @dowSat int
declare @dowSun int
set @dowSat=datepart(dw,'20061028')
set @dowSun=datepart(dw,'20061029')


john.arnott
john.arnott
SSCertifiable
SSCertifiable (6.6K reputation)SSCertifiable (6.6K reputation)SSCertifiable (6.6K reputation)SSCertifiable (6.6K reputation)SSCertifiable (6.6K reputation)SSCertifiable (6.6K reputation)SSCertifiable (6.6K reputation)SSCertifiable (6.6K reputation)

Group: General Forum Members
Points: 6584 Visits: 3059
Nice tip, Divya. I'm adding this to my bookmarks next to that for "Some Common Date Routines" that Lynn Pettis put up a while ago.

---
Edit to correct link
Divya Agrawal
Divya Agrawal
SSCommitted
SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)

Group: General Forum Members
Points: 1818 Visits: 607
Yes Michael, that is certainly there. My post gives an idea of forthcoming weekends without having any intimation of dates.

--Divya
Divya Agrawal
Divya Agrawal
SSCommitted
SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)

Group: General Forum Members
Points: 1818 Visits: 607
Thanks John.
I am not able to open Lynn Pettis post..

--Divya
ramireddydasaradha
ramireddydasaradha
Valued Member
Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)Valued Member (57 reputation)

Group: General Forum Members
Points: 57 Visits: 35
Good Article Divya....

This is Another way..

set datefirst 2
select datediff(dd,5,'12/17/2009')%7
set datefirst 5
select datediff(dd,5,'12/17/2009')%7
Satnam Singh
Satnam Singh
SSCrazy
SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)

Group: General Forum Members
Points: 2060 Visits: 1084
Excellent Article Divya.....
Satnam
davidgr144
davidgr144
SSC Rookie
SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)SSC Rookie (41 reputation)

Group: General Forum Members
Points: 41 Visits: 27
This is a great tip. Thanks.

Can I raise a question regarding it though?

Maybe I'm misreading but if the date you pick is Saturday (instead of Thursday in the example - 17th Dec 2009 was a Thursday I believe), unless I'm very much mistaken by the values returned from modulo, the final result after applying the modulo will be zero. Seven needs to be added in this case.
Carlo Romagnano
Carlo Romagnano
SSChampion
SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)

Group: General Forum Members
Points: 11622 Visits: 3489
This code is indipendent from datefirst:
Monday is 0 and Sunday is 6
SELECT cast(cast('20100215' as datetime) as int)%7
SELECT cast(cast('20100216' as datetime) as int)%7
SELECT cast(cast('20100217' as datetime) as int)%7
SELECT cast(cast('20100218' as datetime) as int)%7
SELECT cast(cast('20100219' as datetime) as int)%7
SELECT cast(cast('20100220' as datetime) as int)%7
SELECT cast(cast('20100221' as datetime) as int)%7
SwePeso
SwePeso
SSC-Insane
SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)

Group: General Forum Members
Points: 20277 Visits: 3433
SELECT 1 + DATEDIFF(DAY, 0, '20091217') % 7


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