Get first Sunday of next month using T-SQL

后端 未结 9 2104
梦毁少年i
梦毁少年i 2021-02-09 13:19

Looking for a way to get the date in the format \"11/1/2009\", which would be the first sunday of next month. I want to run this query after the first sunday in october to get

9条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-02-09 14:10

    I reckon that the answer is this

    SELECT  DATEADD(Month, DATEDIFF(Month, 0, GETDATE()) + 1, 0) + 6 - (DATEPART(Weekday,
                      DATEADD(Month,
                          DATEDIFF(Month,0, GETDATE()) + 1, 0))
                      + @@DateFirst + 5) % 7 --FIRST sunday of following month
    

提交回复
热议问题