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


weekday function


weekday function

Author
Message
smknox
smknox
Old Hand
Old Hand (337 reputation)Old Hand (337 reputation)Old Hand (337 reputation)Old Hand (337 reputation)Old Hand (337 reputation)Old Hand (337 reputation)Old Hand (337 reputation)Old Hand (337 reputation)

Group: General Forum Members
Points: 337 Visits: 126

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





Sergiy
Sergiy
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10578 Visits: 11966
Look for DATEPART in BOL.

Check cross links there to be aware of other datetime functions.
smknox
smknox
Old Hand
Old Hand (337 reputation)Old Hand (337 reputation)Old Hand (337 reputation)Old Hand (337 reputation)Old Hand (337 reputation)Old Hand (337 reputation)Old Hand (337 reputation)Old Hand (337 reputation)

Group: General Forum Members
Points: 337 Visits: 126

thanks! so it's DATEPART(dw,'date')





Steve Fine
Steve Fine
Old Hand
Old Hand (370 reputation)Old Hand (370 reputation)Old Hand (370 reputation)Old Hand (370 reputation)Old Hand (370 reputation)Old Hand (370 reputation)Old Hand (370 reputation)Old Hand (370 reputation)

Group: General Forum Members
Points: 370 Visits: 2
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.



Michael Meierruth
Michael Meierruth
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1086 Visits: 2514

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.


dclark
dclark
SSC-Enthusiastic
SSC-Enthusiastic (133 reputation)SSC-Enthusiastic (133 reputation)SSC-Enthusiastic (133 reputation)SSC-Enthusiastic (133 reputation)SSC-Enthusiastic (133 reputation)SSC-Enthusiastic (133 reputation)SSC-Enthusiastic (133 reputation)SSC-Enthusiastic (133 reputation)

Group: General Forum Members
Points: 133 Visits: 1

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




Steve Fine
Steve Fine
Old Hand
Old Hand (370 reputation)Old Hand (370 reputation)Old Hand (370 reputation)Old Hand (370 reputation)Old Hand (370 reputation)Old Hand (370 reputation)Old Hand (370 reputation)Old Hand (370 reputation)

Group: General Forum Members
Points: 370 Visits: 2

"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.





dclark
dclark
SSC-Enthusiastic
SSC-Enthusiastic (133 reputation)SSC-Enthusiastic (133 reputation)SSC-Enthusiastic (133 reputation)SSC-Enthusiastic (133 reputation)SSC-Enthusiastic (133 reputation)SSC-Enthusiastic (133 reputation)SSC-Enthusiastic (133 reputation)SSC-Enthusiastic (133 reputation)

Group: General Forum Members
Points: 133 Visits: 1

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.





Michael Meierruth
Michael Meierruth
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1086 Visits: 2514
So how do you know that 1753-01-01 is a Monday? My guess is that you used DATEFIRST.
dclark
dclark
SSC-Enthusiastic
SSC-Enthusiastic (133 reputation)SSC-Enthusiastic (133 reputation)SSC-Enthusiastic (133 reputation)SSC-Enthusiastic (133 reputation)SSC-Enthusiastic (133 reputation)SSC-Enthusiastic (133 reputation)SSC-Enthusiastic (133 reputation)SSC-Enthusiastic (133 reputation)

Group: General Forum Members
Points: 133 Visits: 1

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.





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