Andresbi
Andresbi

Reputation: 101

Obtaining the date of 2 Saturdays ago and Last Friday

How can I obtain the date of 2 Fridays ago and 2 Saturdays ago (SQL Server 2012+)

For example,

I am looking for an answer like select xyz getdate() ....

Upvotes: 0

Views: 384

Answers (2)

Marcus Vinicius Pompeu
Marcus Vinicius Pompeu

Reputation: 1261

@Andresbi, your examples states 2 Saturdays before and 1 Friday before.

I think this code gives you the result:

declare @dt datetime = '20201020'

select
    (
        /* Previous sunday */
        @dt - datepart(dw, @dt) + 1
        /* Previous saturday */
        - 1
        /* and the saturday before */
        - 7
    ),
    (
        /* Previous sunday */
        @dt - datepart(dw, @dt) + 1
        /* Previous friday */
        - 2
    )

Upvotes: 1

puravidaso
puravidaso

Reputation: 1223

select dateadd(day, -08, dateadd(wk, datediff(wk, 0, getdate()), 0))
select dateadd(day, -10, dateadd(wk, datediff(wk, 0, getdate()), 0))

Upvotes: 0

Related Questions