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