SQL Server : get next relative day of week. (Next Monday, Tuesday, Wed…)

后端 未结 7 1280
青春惊慌失措
青春惊慌失措 2020-12-05 07:52

What I need is a date for the next given day (Monday, Tuesday, Wed...) following today\'s date.

The user is allowed to select what day following they want and that i

7条回答
  •  渐次进展
    2020-12-05 08:34

    I think this is the best way of doing of finding the next Monday

    CONVERT(VARCHAR(11),DateAdd(DAY,case 
         when (DateName(WEEKDAY, NextVisitDate) ='Tuesday') Then 6 
         when (DateName(WEEKDAY, NextVisitDate) ='Wednesday') Then 5 
         when (DateName(WEEKDAY, NextVisitDate) ='Thursday') Then 4
         when (DateName(WEEKDAY, NextVisitDate) ='Friday') Then 3 
         when (DateName(WEEKDAY, NextVisitDate) ='Saturday') Then 2
         when (DateName(WEEKDAY, NextVisitDate) ='Sunday') Then 1
         else 0 end, DateAdd(DAY, DateDiff(DAY, 0,  NextVisitDate), 0)),106) AS Monday,
    

提交回复
热议问题