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