Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

weekday function Expand / Collapse
Author
Message
Posted Friday, January 26, 2007 11:47 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Sunday, August 25, 2013 5:36 PM
Points: 267, 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




Post #340181
Posted Friday, January 26, 2007 11:50 AM
SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Monday, November 3, 2014 4:30 PM
Points: 4,574, Visits: 8,366
Look for DATEPART in BOL.

Check cross links there to be aware of other datetime functions.
Post #340182
Posted Friday, January 26, 2007 12:06 PM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Sunday, August 25, 2013 5:36 PM
Points: 267, Visits: 126

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

 




Post #340190
Posted Monday, January 29, 2007 9:40 AM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Monday, September 17, 2007 8:44 AM
Points: 330, Visits: 1
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.


Post #340531
Posted Monday, January 29, 2007 10:00 AM
Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Today @ 7:36 AM
Points: 546, Visits: 2,147

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.

Post #340538
Posted Monday, January 29, 2007 3:01 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Wednesday, January 31, 2007 11:45 AM
Points: 105, 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



Post #340659
Posted Monday, January 29, 2007 3:44 PM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Monday, September 17, 2007 8:44 AM
Points: 330, Visits: 1

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




Post #340676
Posted Monday, January 29, 2007 7:05 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Wednesday, January 31, 2007 11:45 AM
Points: 105, 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.

 




Post #340726
Posted Monday, January 29, 2007 11:56 PM
Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Today @ 7:36 AM
Points: 546, Visits: 2,147
So how do you know that 1753-01-01 is a Monday? My guess is that you used DATEFIRST.
Post #340750
Posted Tuesday, January 30, 2007 7:23 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Wednesday, January 31, 2007 11:45 AM
Points: 105, 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.

 

 

 




Post #340832
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse