Simple query to get the Friday date of the previous week

Bobby P 231 Reputation points
2022-06-10T18:19:41.037+00:00

Can I get some help with a simple query to get the Friday date of the previous week?

I just hate doing the week and day math.

Thanks for your review and am hopeful for a solution.

Developer technologies | Transact-SQL
{count} votes

4 answers

Sort by: Most helpful
  1. Viorel 122.6K Reputation points
    2022-06-10T18:35:28.767+00:00

    Which day is the first day of the week?

    If it is Monday, then try a query:

    set datefirst 1  
    select cast( dateadd(day, -(datepart( weekday, getdate()) + 2), getdate()) as date)  
    

    If it is Sunday, then:

    set datefirst 7  
    select cast( dateadd(day, -(datepart( weekday, getdate()) + 1), getdate()) as date)  
    
    1 person found this answer helpful.
    0 comments No comments

  2. KA 76 Reputation points
    2022-06-10T18:21:52.133+00:00
    0 comments No comments

  3. Bert Zhou-msft 3,436 Reputation points
    2022-06-13T02:13:05.017+00:00

    Hi,@Bobby P

    Try this:

    select DATEADD(day, -3 ,DATEADD(week, DATEDIFF(week,0,GETDATE()), 0)) LastFridayStartTime  
    select DATEADD(ms,-3,(DATEADD(day, -2 ,DATEADD(week, DATEDIFF(week,0,GETDATE()), 0)))) LastFridayEndTime  
    

    210627-image.png

    Best regards,
    Bert Zhou


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our Documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  4. Jingyang Li 5,896 Reputation points Volunteer Moderator
    2022-06-15T14:28:44.827+00:00

    The following code will work for you:
    --SET DATEFIRST 1;
    ----SET DATEFIRST 7; ---1--(7 Sunday default)

    declare @dt datetime=getdate()  
       
      
     Select Cast(DATEADD(dd, -3-(((DATEPART(dw,@dt)+@@DATEFIRST) % 7)+5) % 7 , @dt) as date)  LastFriday  
      
      
     --SET DATEFIRST 7;  
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.