Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 last friday of a given date

Author  Topic 

arkiboys
Master Smack Fu Yak Hacker

1433 Posts

Posted - 2012-12-27 : 05:51:56
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
Master Smack Fu Yak Hacker

5155 Posts

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

arkiboys
Master Smack Fu Yak Hacker

1433 Posts

Posted - 2012-12-27 : 07:07:18
Many thanks
Go to Top of Page

bandi
Master Smack Fu Yak Hacker

2242 Posts

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


--
Chandu
Go to Top of Page
   

- Advertisement -