title: Change all columns in table of a certain data type in PostgreSQL 9.6
body: <p>It seems like several months ago I came across a SO question covering this but I can't seem to find it now.</p>
  
  <p>Basically, I want to do two things.</p>
  
  <p>First, a number of tables were made with several columns <code>numeric(20,2)</code> and I want to just change them all to <code>numeric</code>. The statement is simple enough for one column:</p>
  
  <pre><code>ALTER TABLE table_name
  ALTER COLUMN code
  TYPE numeric;
  </code></pre>
  
  <p>Takes care of that.</p>
  
  <p>Second, on these columns I want to remove any trailing zeros:</p>
  
  <pre><code>UPDATE table_name
  SET code = replace(replace(code::text, '.50', '.5'), '.00', '')::numeric;
  </code></pre>
  
  <p>Having difficulty figuring out how to automate it so I only have to specify the table and it will clean up the table. Pretty sure this is possible.</p>