Groups | Blog | Home
all groups > sql server programming > december 2006 >

sql server programming : How can I achieve this


Chris
12/28/2006 8:21:00 PM
Hi,
How can I get the current Thursday of the week date if I run a query from
Sunday to Saturday?

i.e...If i run the query on Mon,Tue,...or Fri,Sat, I should display the
current week's Thusday date.

I don't have a calender table at the moment.

Razvan Socol
12/28/2006 11:25:20 PM
[quoted text, click to view]

If your week is from Sunday to Saturday, you can use a query like this:

SELECT d - (DATEPART(weekday,d) + @@DATEFIRST - 1) % 7 + 4
FROM YourTable

If your week is from Monday to Sunday, you can change it like this:

SELECT d - (DATEPART(weekday,d) + @@DATEFIRST - 2) % 7 + 4
FROM YourTable

Note: I have used the @@DATEFIRST function to make the query
independent of the configuration for SET DATEFIRST.

Razvan
AddThis Social Bookmark Button