In PostgreSQL I want to concat the current_timestamp with an interval as follows:
select current_timestamp + interval 2||' days'
But when I do, I get an error:
[Err] ERROR: syntax error at or near "2"
LINE 1: select current_timestamp + interval 2||' days'
But if I do it like this, it works correctly:
select current_timestamp + interval '2 days'
Why does one work, but not the other?
With reference to the following page
http://www.postgresql.org/docs/8.0/static/functions-datetime.html
Part of the problem is that the standard SQL expression for intervals quotes the number, but not the keywords. So you have to be careful.
In PostgreSQL, quoting like ‘2 day’ and ‘2 days’ also works. So you might think that ‘2’ || ‘ days’ would be equivalent, but it’s not.
The solution, as A.H. said, is to cast the result string as an interval.
You can also use a variable in place of 2. This generates a calendar for 2012.
I use that final cast to date, because date + interval returns a timestamp.