January 26, 2007 at 11:47 am
Does SQL have a function that returns the weekday of a date? In MS Access Weekday(1/26/07) will return 6. Is there something similar in SQL?
THanks,
Sam
January 26, 2007 at 11:50 am
Look for DATEPART in BOL.
Check cross links there to be aware of other datetime functions.
_____________
Code for TallyGenerator
January 26, 2007 at 12:06 pm
thanks! so it's DATEPART(dw,'date')
January 29, 2007 at 9:40 am
Double check that the Day of the Week is the correct one for how you are using this. Some systems use Sunday as the 1st day (Default) and others use Monday. Just set for your needs to ensure it returns what you are looking for.
January 29, 2007 at 10:00 am
Play it safe - wherever you are in the World! Whenever I deal with datepart(dw,...) I capture what it returns for a known date and take it from there. For example:
declare @dowSat int
declare @dowSun int
-- capture these for known dates as they may vary based on SET DATEFIRST
set @dowSat=datepart(dw,'2006-10-28')
set @dowSun=datepart(dw,'2006-10-29')
If you then want to check a date for a Saturday or Sunday you compare it with @dowSat or @dowSun.
January 29, 2007 at 3:01 pm
The value that datepart(DW,MyDate) returns depends on the setting of datefirst.
This code will always return a value of 0 for Monday, 1 for Tuesday,..., 6 for Sunday, no matter what the setting of datefirst is.
select datediff(dd,'17530101',MyDateColumn)%7
If you want your week to start with 0 for Sunday, 1 for Monday, etc., use this:
select (datediff(dd,'17530101',MyDateColumn)+1)%7
If you want the weekdays to be numbered from 1 to 7, instead of 0 to 6, add 1 to the results above:
select (datediff(dd,'17530101',MyDateColumn)%7)+1
January 29, 2007 at 3:44 pm
"This code will always return a value of 0 for Monday, 1 for Tuesday,..., 6 for Sunday, no matter what the setting of datefirst is."
Why would BOL seem to refute what you are saying?
From BOL
The week (wk, ww) datepart reflects changes made to SET DATEFIRST. January 1 of any year defines the starting number for the week datepart, for example: DATEPART(wk, 'Jan 1, xxxx') = 1, where xxxx is any year.
The weekday (dw) datepart returns a number that corresponds to the day of the week, for example: Sunday = 1, Saturday = 7. The number produced by the weekday datepart depends on the value set by SET DATEFIRST, which sets the first day of the week.
January 29, 2007 at 7:05 pm
That is the point of the code I posted. Since it doesn't use the DATEPART function, the setting of DATEFIRST has no effect on it.
It finds the day of the week by calculating the difference in days between 1753-01-01 and your date, and then using that result finding the modulus of 7. Since 1753-01-01 is a Monday, the modulus will return a 0 for Monday, 1 for Tuesday, through a 6 for Sunday.
January 29, 2007 at 11:56 pm
So how do you know that 1753-01-01 is a Monday? My guess is that you used DATEFIRST.
January 30, 2007 at 7:23 am
I guess I just know because it is the very first date that SQL Server supports, and I only had to look it up one time.
In any case, DATENAME(dw,'17530101') would be a better built in function, since it also does not depend of the setting of datefirst. DATENAME does depend on the language setting though.
January 30, 2007 at 8:24 pm
01/01/1900 was also a Monday... kind of convenient, too, because the decimal equivelent for that date is "0".
--Jeff Moden
Change is inevitable... Change for the better is not.
January 30, 2007 at 9:35 pm
Yes, but 1900-01-01 is not good for the code I posted, because dates earlier than 1900-01-01 would return a negative modulus.
Since there is no SQL Server datetime before 1753-01-01, there is no negative modulus to worry about, and it will work with any datetime value with any setting of DATEFIRST and any language setting.
February 1, 2007 at 5:54 pm
Yep... clever code. Thanks.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 13 posts - 1 through 12 (of 12 total)
You must be logged in to reply to this topic. Login to reply