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
    Commented Sep 7, 2020 at 14:55
  • @a_horse_with_no_name There might have been some custom implicit type casts. Commented 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"? Commented 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
    Commented 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
    Commented 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 Commented Sep 9, 2020 at 10:27

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.