Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Most recent # day

Status
Not open for further replies.

waldopepper

Programmer
Nov 21, 2003
124
GB
Can anyone help us with a bit of code -

I want to determine the most recent friday (previous) to a supplied date in a stored procedure.

So if I a date (e.g. 15th June 2005), I need the stored procedure to return most recent previous Friday's date (10th June 2005) - all in datetime format.

I can probably work this out in a cursor but this might get long-winded and wondered if there's a simpler way I'm not thinking of.

Can anyone help?

thanks
w
 
You can take advantage of the DatePart function. With the dw argument specified, you will get the 'Day of week' number. subtract one for to get Friday and you're all set.

select GetDate(), GetDate() - DatePart(dw, GetDate()) - 1

hope this helps.

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Something like this:

Code:
SET DATEFIRST 6

DECLARE @date datetime
SET @date = '20050615'

SELECT @date - DATEPART(dw, @date) AS LastFriday

--James
 
Awesome - works like a dream. Thanks for the swift response guys.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top