0

I’m using Postgres 9. I’m trying to do date math with a column in my table that is an integer. I’m trying this:

select current_timestamp + interval age || ' years'
from my_table
where age is not null
limit 5;
ERROR:  syntax error at or near "||"
LINE 1: select current_timestamp + interval age || ' years' from rac...

What is the proper way to write this? I’m trying to add the age column, which is in years, to the current timestamp (now)?

Erwin Brandstetter
  • 605,456
  • 145
  • 1,078
  • 1,228
Dave
  • 15,639
  • 133
  • 442
  • 830
  • Ain't no such thing as "PostGresql 9". I fixed the name, the version is still undefined. Consider [naming](https://wiki.postgresql.org/wiki/Identity_Guidelines) and [versioning guidelines](https://www.postgresql.org/support/versioning) of the project. – Erwin Brandstetter Jul 01 '16 at 22:10

1 Answers1

0

Multiply your integer with 1-year intervals and add it to the timestamp:

SELECT current_timestamp + interval '1 year' * age
FROM   my_table
WHERE  age IS NOT NULL
LIMIT  5;

Related:

Community
  • 1
  • 1
Erwin Brandstetter
  • 605,456
  • 145
  • 1,078
  • 1,228