SQL DATEPART(dw,date) need monday = 1 and sunday = 7

后端 未结 12 568
[愿得一人]
[愿得一人] 2020-12-23 16:27

I have a Query where I get the WeekDay of a date but by default:

  • Sunday = 1

  • Moday = 2

  • etc.

12条回答
  •  生来不讨喜
    2020-12-23 17:19

    This will do it.

    SET DATEFIRST 1;
    
    -- YOUR QUERY
    

    Examples

    -- Sunday is first day of week
    set datefirst 7; 
    select DATEPART(dw,getdate()) as weekday
    
    
    -- Monday is first day of week
    set datefirst 1;
    select DATEPART(dw,getdate()) as weekday
    

提交回复
热议问题