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
Domanda
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
Soluzione
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