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

后端 未结 12 622
[愿得一人]
[愿得一人] 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:11

    You can use a formula like:

    (weekday + 5) % 7 + 1
    

    If you decide to use this, it would be worth running through some examples to convince yourself that it actually does what you want.

    addition: for not to be affected by the DATEFIRST variable (it could be set to any value between 1 and 7) the real formula is :

    (weekday  + @@DATEFIRST + 5) % 7 + 1
    

提交回复
热议问题