Get the most recent Friday's date SQL

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

    you can check if the current day of week is friday or greater DATEPART(dw,GETDATE()) and then call (SELECT DATEADD(wk, DATEDIFF(wk,0,GETDATE()), 0)+4) or (SELECT DATEADD(wk, DATEDIFF(wk,0,GETDATE()), 0)-3)

    SELECT 
    CASE WHEN DATEPART(dw,GETDATE()) >= 5 THEN 
    (SELECT DATEADD(wk, DATEDIFF(wk,0,GETDATE()), 0)+4) 
    ELSE 
    (SELECT DATEADD(wk, DATEDIFF(wk,0,GETDATE()), 0)-3) 
    END
    

提交回复
热议问题