Skip to content
Advertisement

How do I use a prior query’s result when subtracting an interval in Postgres?

I have the following code:

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.

For this specific example, you don’t need to use an interval. You can subtract an integer directly from a date.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement