title: IF function in PostgreSQL as in MySQL
body: <p>I am trying to replicate the <code>IF</code> function from MySQL into PostgreSQL.</p>
  
  <p>The syntax of <code>IF</code> function is <code>IF(condition, return_if_true, return_if_false)</code></p>
  
  <p>I created following formula:</p>
  
  <pre><code>CREATE OR REPLACE FUNCTION if(boolean, anyelement, anyelement)
     RETURNS anyelement AS $$
  BEGIN
      CASE WHEN ($1) THEN
      RETURN ($2);
      ELSE
      RETURN ($3);
      END CASE;
      EXCEPTION WHEN division_by_zero THEN
      RETURN ($3);
  END;
  $$ LANGUAGE plpgsql;
  </code></pre>
  
  <p>It works well with most of the things like <code>if(2&gt;1, 2, 1)</code> but it raises an error for:</p>
  
  <pre><code>if( 5/0 &gt; 0, 5, 0)
  </code></pre>
  
  <blockquote>
    <p>fatal error division by zero.</p>
  </blockquote>
  
  <p><strong>In my program I can't check the denominator as the condition is provided by user.</strong></p>
  
  <p>Is there any way around? Maybe if we can replace first parameter from boolean to something else, as in that case the function will work as it will raise and return the exception.</p>