viniciussss viniciussss - 1 year ago 74
SQL Question

Prepared statement with date added by interval as parameter in Postgresql

This works:

String query = "select DATE '2016-03-16' + interval '7 days'";

But I want to have '2016-03-16' and 7 as parameters in a prepared statement. How to do it?

I tried like this, but it didn't work:

String query = "select DATE ? + interval ?";
Object param[] = {"2016-03-16", "7 days"};

Answer Source

Try something like:

select to_date(?, 'YYYY-MM-DD') + cast(? as interval)

Other option is to use something like

select cast(? as date) + cast(? as interval)

but it depends on current locale for date conversion