Get first Sunday of next month using T-SQL

后端 未结 9 2132
梦毁少年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:05

    Here is a query to get first working day of next month

    DECLARE @DAYOFWEEK INT,@ReminderDate DateTime
    SET @DAYOFWEEK = DATEPART( WEEKDAY,DateAdd(D,- Day(GetDate())+1, DATEADD(M,1,GetDate())) )
    Print @DAYOFWEEK
    If @DAYOFWEEK = 1
    Set @ReminderDate = DateAdd(D,- Day(GetDate())+2, DATEADD(M,1,GetDate()))
    Else If @DAYOFWEEK =7
    Set @ReminderDate = DateAdd(D,- Day(GetDate())+3, DATEADD(M,1,GetDate()))
    Else
    Set @ReminderDate = DateAdd(D,- Day(GetDate())+1, DATEADD(M,1,GetDate()))
    Print @ReminderDate
    

提交回复
热议问题