How to do a date older than search in Postgres using a parameterized value

I had a situation where I needed to do a search for records with a date older than X days. But the value of X is from a text component in the app. So it must be able to be parameterized.

All well and good, but the Postgres syntax for doing such a search is kinda odd:

where processeddate < NOW() - INTERVAL '365 days'

You cannot do this as it cannot be correctly parameterized:

where processeddate < NOW() - INTERVAL '{{txtAge.value}} days'

But this does work:

where processeddate < NOW() - INTERVAL '1 day' * {{txtAge.value}}

2 Likes

Thank you Bradly! I also tried doing what didn't work for you. Now using your solution! Awesome you posted about it!