Nick
Nick

Reputation: 515

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.

Upvotes: 0

Views: 956

Answers (1)

Caius Jard
Caius Jard

Reputation: 74605

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

Upvotes: 1

Related Questions