0

When I execute these script, it works in postgresql;

select '2021-09-01'::Date - 1

select current_date -1 

But when I use this type of subtracting in function, postgresql gives me error like this;

Function usage in declare part :

v_date date := p_date - p_number;
ERROR:  operator does not exist: date - numeric
3
  • What result do you expect if you pass 3.14 as p_number? Commented Oct 7, 2021 at 13:48
  • The function will not take a decimal number as input, so I don't have any expectations regarding 3.14. So "v_date date := p_date - p_number::int;" works for me. Commented Oct 7, 2021 at 14:23
  • The cleaner solution is then to declare the parameter as int Commented Oct 7, 2021 at 14:24

1 Answer 1

1

Try:

v_date date := p_date - p_number::int;.

Per Date/Time Operators you can only subtract an integer, interval or date from a date.

Note: the ::int cast will round the numeric number.

UPDATE

Another option is to make p_number be an integer from the start, though this depends on whether it is being used for other purposes where it needs to be numeric.

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

1 Comment

See UPDATE for an alternative solution.

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.