문제

Looking for code in SQL SERVER 2012 to find out Next occurrence of 'WEDNESDAY' for all ORDERDATEs from ORDERS Table. I came up with the following, but it just gives the Orderdate + 7 days. Kindly help me on this.

SELECT DATEADD(DD,7, CONVERT(Datetime, OrderDate, 101))
FROM Orders
도움이 되었습니까?

해결책

You can use this:

SELECT CASE WHEN DATENAME(WEEKDAY,OrderDate) = 'Wednesday' 
       THEN DATEADD(DAY,7,OrderDate)
       ELSE DATEADD(DAY,(18-(@@DATEFIRST+DATEPART(WEEKDAY,OrderDate)))%7,OrderDate)
       END
FROM Orders
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top