r/SQL • • May 03 '22

MS SQL Reoccurring query, no hard coding, MS SQL

I am looking for a WHERE clause to set a reoccurring query to be run for the past 2 weeks. No hard coding can be used. Anyone have any ideas?

Have tried “>= getdate() -14 “ and that’s not pulling how I want. Any suggestions help.

1 Upvotes

18 comments sorted by

View all comments

Show parent comment

1

u/fatandgeared8675309 May 04 '22

Well shit lol. Could switch to 2 and weeks?

1

u/alinroc SQL Server DBA May 04 '22

"2 weeks" yields the same problem as 14 days - it'll be exactly two weeks prior to this point in time. If you want the full day that was two weeks ago, you need field >= dateadd(day,-14, cast(getdate() as date))

1

u/fatandgeared8675309 May 04 '22

Ok thanks, I’ll give that a try. I’m fairly to SQL so some of these things are still a bit over my head.

1

u/locesterines May 04 '22

Or if using SQL 2005 :), dateadd(day, -14, cast(convert(varchar(10),getdate(),101) as datetime))