Get the most recent Friday's date SQL

后端 未结 8 1232
孤城傲影
孤城傲影 2021-02-13 00:48

I\'m trying to get the most recent Friday in SQL Server 2008.

I have this. It gets the beginning of the week (monday) then subtracts 3 days to get Friday.



        
8条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-13 01:19

    The other solutions were not working for my use case.

    This works for finding any previous day by replacing 'Sunday' with the day you`re looking for:

        DECLARE @myDate DATE = GETDATE()
    
        WHILE DATENAME(WEEKDAY, @myDate) <> 'Sunday'
        BEGIN
            SET @myDate = DATEADD(DAY, -1, @myDate)
        END
    

提交回复
热议问题