Get first Sunday of next month using T-SQL

后端 未结 9 2126
梦毁少年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 13:58

    You can use DATENAME to determine the day you want, I might recommend a loop to move the date from the 01 of the month in question to get to the first sunday.

    So lets try:

    DECLARE @DateTime DATETIME
    

    Set to the date to start off with, then add 1 day until you find what you are looking for. Use datename with dw...

    We have used this to determine weekends, but holidays will be a problem, where we use a table to store that.

提交回复
热议问题