0

I'm trying a SQL query with the function COALESCE in PostgreSQL 9.6.17, but it produces an error:

invalid syntax for type double precision: ""

Example query:

SELECT COALESCE(date_part('year', s.date_pp),'')
FROM public.sendoc s.

This query is working on PostgreSQL 9.3.17.

How to get around the error at the server DB level without changing the source code of the program?

2
  • 1
    I am really surprised this worked in 9.3
    – user1822
    Sep 7, 2020 at 14:55
  • @a_horse_with_no_name There might have been some custom implicit type casts. Sep 8, 2020 at 2:56

1 Answer 1

1

I don't think you can "fix" this without changing your query or going back to Postgres 9.3, where apparently type casting rules were more relaxed.

date_part() returns a floating point value, and in the absence of an implicit type cast Postgres tries to convert the other operand of coalesce() to a compatible value, and an empty string ('') cannot be so converted.

4
  • Thanks for the answer. As I understand it, in the Postgres settings, there is no way to configure loyalty when an implicit type for resolve "fix"? Sep 7, 2020 at 20:03
  • In my view, it was a bug that was fixed post-9.3. I don't know of a way to undo that in Postgres. If you're looking for a database that's quite liberal with type casts, try MySQL (though it'll probably hurt you in many other ways).
    – mustaccio
    Sep 7, 2020 at 22:32
  • @ИванЛинюшин: the correct fix is, to fix your query, not Postgres (because 9.6 behaves correctly, the fact that this worked in 9.3 was a bug to begin with).
    – user1822
    Sep 9, 2020 at 5:53
  • I'm using PostgreSQL CAST To Convert a Value of One Type to Another. SELECT COALESCE(CAST(date_part('year', s.date_pp) as VARCHAR),'') FROM public.sendoc s Sep 9, 2020 at 10:27

Your Answer

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

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