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

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

    Looks like the DATEFIRST settings is the only way, but it's not possible to make a SET statement in a scalar/table valued function. Therefore, it becomes very error-prone to the colleagues following your code. (become a trap to the others)

    In fact, SQL server datepart function should be improved to accept this as parameter instead.

    At the meantime, it looks like using the English Name of the week is the safest choice.

提交回复
热议问题