title: How to get multiple comma-separated values as individual columns?
body: <p>I have a table with data like this:</p>
  
  <pre><code>select * from data
  
  id |  col1 |  col2 |  col3
  ---+-------+-------+-------
   1 | 1,2,3 | 4,5,6 | 7,8,9
  </code></pre>
  
  <p>I want to get the data like this:</p>
  
  <pre><code>id | name | dd | fn | suf
  ---+------+----+----+-----
  1  | col1 |  1 |  2 |  3
  1  | col2 |  4 |  5 |  6
  1  | col3 |  7 |  8 |  9
  </code></pre>
  
  <p>Currently, I use <a href="http://www.postgresql.org/docs/current/interactive/functions-string.html" rel="nofollow noreferrer">split_part()</a> in a query like this:</p>
  
  <pre><code>SELECT * from(
  select id,
         'col1' as name,
         NULLIF(split_part(col1, ',', 1), '') AS dd, 
         NULLIF(split_part(col1, ',', 2), '') AS fn, 
         NULLIF(split_part(col1, ',', 3), '') AS suf
         from data
  
         UNION 
         select id,
         'col2' as name,
         NULLIF(split_part(col2, ',', 1), '') AS dd, 
         NULLIF(split_part(col2, ',', 2), '') AS fn, 
         NULLIF(split_part(col2, ',', 3), '') AS suf
         from data
          UNION 
         select id,
         'col3' as name,
         NULLIF(split_part(col3, ',', 1), '') AS dd, 
         NULLIF(split_part(col3, ',', 2), '') AS fn, 
         NULLIF(split_part(col3, ',', 3), '') AS suf
         from data
  );
  </code></pre>
  
  <p>Is there a more elegant way? <strong>I have 20 columns</strong>.</p>