Question

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
Était-ce utile?

La solution

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
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top