Get the most recent Friday's date SQL

后端 未结 8 1218
孤城傲影
孤城傲影 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:22

    Here is a completly set oriented way to achive the last Friday:

    select Friday from
    (
    select max(GetDate()) as Friday where datepart(dw, getdate()) = 6
    union all
    select max((GetDate() - 1)) where datepart(dw, (getdate() - 1)) = 6
    union all
    select max((GetDate() - 2)) where datepart(dw, (getdate() - 2)) = 6
    union all
    select max((GetDate() - 3)) where datepart(dw, (getdate() - 3)) = 6
    union all
    select max((GetDate() - 4)) where datepart(dw, (getdate() - 4)) = 6
    union all
    select max((GetDate() - 5)) where datepart(dw, (getdate() - 5)) = 6
    ) x where Friday is not null
    

提交回复
热议问题