How to get previous friday in SQL Server?
Here is the code I have so far:
select (7 -datePart(dw, getdate()+3)) +1
How to get previous friday in SQL Server?
Here is the code I have so far:
select (7 -datePart(dw, getdate()+3)) +1
Use mod like this:
declare @date datetime = getdate()
declare @dow int = datepart(dw,@date)
declare @mod int = @dow % 7 +1
select cast(dateadd(d,-@mod ,@date) as date)
If your week starts on Sunday, then here's a one-liner:
SELECT DATEADD(WEEK
,DATEDIFF(WEEK, '1900-01-05', DATEADD(DAY, 6 - DATEPART(DAY, GETDATE()) - 1, GETDATE()))
,'1900-01-05') AS [Last Friday]
The query counts the number of weeks since 1900-01-05 (which is a Friday) to today's date minus 1 week. This number of weeks is added to 1900-01-05 to get the previous week's Friday.
Try this select convert(varchar(10), DATEADD(DD, -1 - DATEPART(DW, CONVERT(DATETIME, FLOOR(CONVERT(FLOAT, GETDATE()))) ),CONVERT(DATETIME, FLOOR(CONVERT(FLOAT, GETDATE())))),101) It will give you previous friday of every week