weekday function

  • 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

  • Look for DATEPART in BOL.

    Check cross links there to be aware of other datetime functions.

    _____________
    Code for TallyGenerator

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

     

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

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

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

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

     

  • So how do you know that 1753-01-01 is a Monday? My guess is that you used DATEFIRST.

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

     

     

     

  • 01/01/1900 was also a Monday... kind of convenient, too, because the decimal equivelent for that date is "0".

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

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

     

  • Yep... clever code.  Thanks.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

Viewing 13 posts - 1 through 12 (of 12 total)

You must be logged in to reply to this topic. Login to reply