I have the following code:
WITH example_var AS (SELECT 3) SELECT current_date - interval '3 day'
How do I use example_var
in in the interval?
I’d like to do something like - interval CONCAT(example_var, 'day')
so that I could change what example_var
is equal to and therefore change the length of interval but that isn’t working.
Advertisement
Answer
If you want to create an interval from a constant value, you can use the make_interval()
function.
But you need to include example_var
in the FROM clause. But can’t refer to the actual value, you need to define a column alias for that as well.
WITH example_var (days) AS ( values (3) ) SELECT current_date - make_interval(days => v.days) from example_var v
For this specific example, you don’t need to use an interval. You can subtract an integer directly from a date.
WITH example_var (days) AS ( values (3) ) SELECT current_date - v.days from example_var v