Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

SQL Add Integer Column to Date column

This works:

SELECT "date", "value"::Int, "date" + interval '1' day AS "computed" 
FROM "tab1"

but what if I want to add a variable from the table to a timestamp?

SELECT "date", "value"::Int, "date" + interval "value" day AS "computed" 
FROM "tab1"

This fails.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

An interval is like a number, and can be multiplied

SELECT "date", "value"::Int, "date" + interval '1' day * "value" AS "computed" 
FROM "tab1"

Not sure if your cast to int was indicative of it being e.g. some decimal like 1.5, but if it is then 1.5 days would be added. If you want just the integral part of value to matter, then consider something like "date" + interval '1' day * "value"::int

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading