0

I want to do the following comparison :

IF ( V_C_CRITERE2.DATE_FIN_SOUHAITEE - TO_DATE(V_SEUIL_ALERT) < CURRENT_DATE) THEN

DATE_FIN_SOUHAITEE is a DATE.

But a get this error :

operator does not exist: interval < date 

How can i do the comparison?

12
  • What data type is V_SEUIL_ALERT?
    – user1822
    Commented Apr 22, 2020 at 10:35
  • it's a VARCHAR(100) Commented Apr 22, 2020 at 10:36
  • Storing date values in a varchar column is a huge mistake to begin with. But given the error message it seems that DATE_FIN_SOUHAITEE is really a timestamp. But in any case: your expression doesn't make sense. Subtracting one date from another returns an integer (=number of days) or if one of them is a timestamp, the result is an interval (e.g. 3 days 16 hours 10 minutes 5 seconds). Comparing that to a date value (2020-04-22) doesn't make sense. What exactly are you trying to achieve with that?
    – user1822
    Commented Apr 22, 2020 at 10:40
  • What i want to do is to compare the DATE which i will get from : V_C_CRITERE2.DATE_FIN_SOUHAITEE - TO_DATE(V_SEUIL_ALERT) with the CURRENT_DATE .What changes can i do to make to comparison work ?? Commented Apr 22, 2020 at 10:51
  • 1
    That expression doesn't make sense in Oracle either if both columns are DATEs. The only thing that would make sense is if V_SEUIL_ALERT was a number (not a date)
    – user1822
    Commented Apr 22, 2020 at 11:01

1 Answer 1

1

Although the question states that DATE_FIN_SOUHAITEE is a DATE, the error message "operator does not exist: timestamp without time zone - numeric" makes it clear that it really is a timestamp. In Postgres - unlike Oracle - a timestamp is something substantially different than a date (in Oracle they are more or less the same thing - they just differ in the precision).

In Postgres you can only subtract an interval from a timestamp, and you can subtract integer values from a date

If V_SEUIL_ALERT is indeed a varchar that stores a number that represents years, you need to convert that string value to a proper interval in order to be able to subtract it from a timestamp:

V_C_CRITERE2.DATE_FIN_SOUHAITEE - make_interval(years => V_SEUIL_ALERT::integer)

the above can then be compared using < with current_date.

I don't know what the original code in Oracle did, because DATE_FIN_SOUHAITEE - V_SEUIL_ALERT would implicitly convert V_SEUIL_ALERT to a number which is then taken as days to be subtracted from DATE_FIN_SOUHAITEE, not years.

If that column indeed stores days rather than years (as stated in the comments), you need to use:

make_interval(days => V_SEUIL_ALERT::integer)

You should really take the opportunity of that migration and fix the wrong data type for the column V_SEUIL_ALERT to be an integer, numeric or even interval if that is what you use it for. Never store numbers in VARCHAR columns. That was already a really, really bad idea in Oracle.

1
  • Thank you for your time and explanations. It's working now Commented Apr 22, 2020 at 11:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.