简体   繁体   中英

Calculate the previous sundays date from a Date column?

I have a table that contains a field t.Processed_Date. I need to return the Previous sunday of that date. Everything I'm trying is not working.

DATEADD(day,
    -1 - (DATEPART(weekday, GETDATE()) + CAST(Processed_Date As date) - 2) % 7,
    GETDATE()
) As 'Last Sunday'

But this gives an error

Msg 206, Level 16, State 2, Line 3
Operand type clash: date is incompatible with int

Server is MS SQL Server 2017 standard

SELECT DATEADD(wk, DATEDIFF(wk, 6, Processed_Date), 6) as LastSunday

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM