0

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.

1
  • date is a date value then you can simply use "date" + "value" Commented Dec 2, 2021 at 23:04

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

FYI there is also a make_interval(days => value) function. postgresql.org/docs/current/functions-datetime.html
There is, though not in oracle (but this works there too) - too much to keep in my brain about the differences between db vendors; I'll take any common ground I can remember! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.