SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 last friday of a given date
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

arkiboys
Flowing Fount of Yak Knowledge

1416 Posts

Posted - 12/27/2012 :  05:51:56  Show Profile  Reply with Quote
Hi,
for a given date i.e. 31 Dec 2012, how can I get the last friday of this date which is: 28 Dec 2012


Thanks

sunitabeck
Flowing Fount of Yak Knowledge

5155 Posts

Posted - 12/27/2012 :  06:30:43  Show Profile  Reply with Quote
DECLARE @dt DATETIME = '20121231'
SELECT DATEADD(dd,-DATEDIFF(dd,-3,@dt)%7,@dt)
Go to Top of Page

arkiboys
Flowing Fount of Yak Knowledge

1416 Posts

Posted - 12/27/2012 :  07:07:18  Show Profile  Reply with Quote
Many thanks
Go to Top of Page

bandi
Flowing Fount of Yak Knowledge

India
2210 Posts

Posted - 12/27/2012 :  07:12:34  Show Profile  Reply with Quote
--Alternate is
DECLARE @date DATETIME = '20121201' -- Thursday
SELECT DATEADD(DAY,-(DATEDIFF(DAY,'19000105',@date)%7),@date)


--
Chandu
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.03 seconds. Powered By: Snitz Forums 2000