title: How to identify if a value in column can be encoded to Latin in Postgres
body: <p>My Postgres database encodes everything as UTF-8, but in a query when selecting a column I want to know if it can be encoded as Latin. I've no need to actually encode it as Latin but I need to know if can be encoded as Latin.</p>
  
  <p>By Latin in mean what other people generally mean by Latin, i.e characters are recognisable to Western European speakers   </p>
  
  <p>i.e </p>
  
  <pre><code>SELECT val 
  FROM 
  TABLE1
  WHERE IS_LATIN(Val);
  </code></pre>
  
  <p><strong>Solution</strong></p>
  
  <p>I used the answer posted below, firstly I tried the python function but it failed because I dont have that language installed. Then I tried the pl/sql function and that failed because missing RETURN statement, but I fixed as follows and now works okay</p>
  
  <pre><code>CREATE OR REPLACE FUNCTION is_latin(input text)
  RETURNS boolean
  LANGUAGE plpgsql
  IMMUTABLE
  AS $$
  BEGIN
    PERFORM convert_to(input, 'iso-8859-15');
    RETURN true;
  EXCEPTION
    WHEN untranslatable_character THEN
      RETURN false;
  END;
  $$;
  </code></pre>