Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL server: How do i get the date of previous Saturday

I need to filter a table from last Saturday 7pm to now. How do i do that?

The column i'm filtering is of dateTime2(0) format?

like image 539
Fearghal Avatar asked Mar 22 '23 02:03

Fearghal


2 Answers

This expression will always give you the previous Saturday at 7pm, independent of your DATEFIRST setting:

select DATEADD(week,
       DATEDIFF(week,'19000101',CURRENT_TIMESTAMP),'1899-12-30T19:00:00')

If it's run on a Saturday, it'll give you the previous Saturday.

like image 145
Damien_The_Unbeliever Avatar answered Apr 06 '23 04:04

Damien_The_Unbeliever


SELECT * From TableName Where MyDate >= DATEADD(HH,19, DATEADD(DAY, (DATEDIFF(DAY, '19000106', GetDate()) - 1) / 7 * 7, '19000106'))

like image 32
Fred Avatar answered Apr 06 '23 03:04

Fred