Parameterizing SQL Query with Date

Viewed 28

I am trying to implement parameter for my SQL query which will be executed using SQL Server agent Job on a scheduled basis. This parameter which sets the cutoff period (x) in days and job should load any changed records in last x days. On the other hand, if parameter is zero it should load everything.

Currently, I am having a where condition as follows:


WHERE updatedate > case when x > 0 then dateadd(day, -x, getdate()) // for parameter eg; x = 2

else getdate() - dateadd(day, -x, getdate()) // for parameter, x = 0

Is this logically correct to use or is there any better solution that anyone can suggest for me?

Any help is much appreciated.

Thank you!

0 Answers
Related