title: AS/400: Most efficient DB2 SQL to unpack EBCDIC subfield values as strings?
body: <p>Most files I am working with only have the following fields: </p>
  
  <ul>
  <li>F00001 - usually 1 (f1) or 9 (f9)</li>
  <li>K00001 - usually only 1-3 sub-fields of
  zoned decimals and ebcdic </li>
  <li>F00002 - sub-fields of ebcdic, zoned and
  packed decimals</li>
  </ul>
  
  <p>Occasionally other field names K00002, F00003 and F00004 will appear in cross reference files.</p>
  
  <p><strong>Example Data:</strong></p>
  
  <pre><code>+---------+--------------------------------------------------+--------------------------------------------------------------------------------------------+
  | F00001  | K00001                                           |  F00002                                                                                    |
  +---------+--------------------------------------------------+--------------------------------------------------------------------------------------------+
  | f1      |  f0  f0  f0  f0  f1  f2  f3  f4  f5  f6  d7  c8  | e2  e3  c1  c3  d2  d6  e5  c5  d9  c6  d3  d6  e7  40 12  34  56  7F  e2  d2  c5  c5  e3  |
  +---------+--------------------------------------------------+--------------------------------------------------------------------------------------------+
  </code></pre>
  
  <p><strong>Currently using:</strong></p>
  
  <pre><code>SELECT SUBSTR(HEX(F00001), 1, 2) AS FNAME_1, SUBSTR(HEX(K00001), 1, 14) AS KNAME_1, SUBSTR(HEX(K00001), 15, 2) AS KNAME_2, SUBSTR(HEX(K00001), 17, 2) AS KNAME_2, SUBSTR(HEX(F00002), 1, 28) AS FNAME_2, SUBSTR(HEX(F00002), 29, 8) AS FNAME_3, SUBSTR(HEX(F00002), 37, 10) AS FNAME_4 FROM QS36F.FILE
  </code></pre>
  
  <p>Is this the best way to unpack EBCDIC values as strings?</p>