champ.exe
champ.exe

Reputation: 125

Calling a function which has a numeric value assigned to it for query

Here instead of using 7 days interval, I want to use days_gap in a function and pass a numeric value to it instead of a hardcoded value. And then directly call it in the SELECT function

SELECT BOO_NUMBER,
      ROO_NUMBER
    FROM BOO B
    WHERE B.FOO < CURRENT_TIMESTAP - '7 days'::interval

Instead of using '7 days'::interval I want to use a function days_gap which takes a numeric value in the function and can be called respectively instead of 7 days interval.

Please help me with the function code to be called in the select query.

Thanks in advance!

Upvotes: 1

Views: 29

Answers (2)

user330315
user330315

Reputation:

Are you looking for make_interval()?

SELECT boo_number,
       roo_number
FROM boo b
WHERE b.foo < current_timestap - make_interval(days => 7);

Note that you can always multiply a one day interval with the desired number of days as well:

interval '1 day' * 7 is the same as interval '7 day'

Upvotes: 0

Georgi Raychev
Georgi Raychev

Reputation: 1334

This should do:

SELECT BOO_NUMBER,
      ROO_NUMBER
    FROM BOO B
    WHERE B.FOO < CURRENT_TIMESTAP - interval '1 day' * days_gap()

Upvotes: 1

Related Questions