0

I'm trying to pass a variable to a select statement using PostgreSQL. The overall goal is to use this logic in a stored procedure where the variable deadline_interval would be an input parameter. Here's what I've tried so far:

DO
$$
DECLARE
  date_deadline date;
  deadline_interval varchar := '6 month';
BEGIN
  date_deadline := (SELECT CURRENT_DATE - INTERVAL deadline_interval);
  RAISE NOTICE 'here: %', date_deadline;
END;
$$

Unfortunately the variable deadline_interval is undefined in the select statement. What am I doing wrong here?

1 Answer 1

1

Use a cast instead of the prefix.

...
date_deadline := (SELECT CURRENT_DATE - deadline_interval::interval);
...

Or, even better, use interval as type for deadline_interval from the beginning.

DO
$$
DECLARE
  date_deadline date;
  deadline_interval interval := INTERVAL '6 month';
BEGIN
  date_deadline := (SELECT CURRENT_DATE - deadline_interval);
  RAISE NOTICE 'here: %', date_deadline;
END;
$$
Sign up to request clarification or add additional context in comments.

Comments

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.