Using this answer to convert a (date + interval) into a date. Only in my case the date and the interval are strings that are the result of slice&dice of other strings. The problem is that it works when I construct the string for the date but not for the interval.
So, the following statements work:
SELECT (date '2013-01-01');
SELECT (interval '53 days');
SELECT (date '2013-01-01' + interval '53 days');
Now I want to synthesize the strings that are passed after date and interval by substring-ing some other string (think stored procedure where we operate on a passed parameter):
This works for date:
SELECT date (substring('2015015' from 1 for 4)||'-01-01')::text;
But it fails for interval:
SELECT interval (substring('2015015' from 5)||' days')::text;
with error message:
ERROR: syntax error at or near "substring"
It actually works if I cast explicitly to interval either with CAST(x as INTERVAL) or equivalently with x::interval:
SELECT CAST((substring('2015015' from 5)||' days')::text AS INTERVAL);
or equivalently:
SELECT ((substring('2015015' from 5)||' days')::text)::interval;
Why does the date TEXT work regardless of how the TEXT is placed there, but the same with interval only works with direct text, but not with synthesized one.
I'm on Postgres 9.4.