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/PossiblePreparation May 03 '22

You can do some amount of obfuscation to share what you've tried. It sounds like you have a document table and an attachments table, and you want to run you date filter against the attachments table and semijoin that. Something like:

select *
from   documents d
where  exists (select null
               from   attachments a
               where  a.doc_id = a.doc_id
               and    a.attached_date >= getdate()-14
              )