How do I cast a string to integer and have 0 in case of error in the cast with PostgreSQL?
- by silviot
In postgres I have a table with a varchar column. The data is supposed to be integers and I need it in iteger type in a query. Some values are empty strings.
The following:
SELECT myfield::integer FROM mytable
yields ERROR: invalid input syntax for integer: ""
How can I query a cast and have 0 in case of error during the cast in postgres?