开发者

PostgreSQL - bind variables and date addition

开发者 https://www.devze.com 2022-12-30 01:23 出处:网络
I need to update some timestamp columns in a table in a Postgres (8.3) database. My q开发者_运维问答uery (simplified) look like this:

I need to update some timestamp columns in a table in a Postgres (8.3) database.

My q开发者_运维问答uery (simplified) look like this:

update table1 set dateA = dateA + interval '10 hours' where id = 1234;

This is part of a script and there's a lot to update so my preference is to use bind variables, rather than to have to build the query string each time. This means my query becomes:

update table1 set dateA = dateA + interval '? hours' where id = ?;

When I do this, the complaint is that I've supplied 2 bind variables when only one is required.

If I try to put the ? outside the quote marks:

update table1 set dateA = dateA + interval ? ' hours' where id = ?;

I get:

... syntax error at or near "' hours'"

It looks as though the query has been interpreted as

... dateA = dateA + interval '10' ' hours' ...

I can't find anything in the documentation to help ... any suggestions?

Thanks


Try this:

update table1 set dateA = dateA + ( (interval '1 hours') * ? ) where id = ?;

Or this:

update table1 set dateA = dateA + cast(? || ' hours' as interval) where id = ?;
0

精彩评论

暂无评论...
验证码 换一张
取 消