title: PostgreSQL ORDER BY issue - natural sort
body: <p>I've got a Postgres <code>ORDER BY</code> issue with the following table:</p>
  
  <pre><code>em_code  name
  EM001    AAA
  EM999    BBB
  EM1000   CCC
  </code></pre>
  
  <p>To insert a new record to the table,</p>
  
  <ol>
  <li>I select the last record with <code>SELECT * FROM employees ORDER BY em_code DESC</code></li>
  <li>Strip alphabets from em_code usiging reg exp and store in <code>ec_alpha</code></li>
  <li>Cast the remating part to integer <code>ec_num</code></li>
  <li>Increment by one <code>ec_num++</code></li>
  <li>Pad with sufficient zeors and prefix <code>ec_alpha</code> again</li>
  </ol>
  
  <p>When <code>em_code</code> reaches EM1000, the above algorithm fails.</p>
  
  <p>First step will return EM999 instead EM1000 and it will again generate EM1000 as new <code>em_code</code>, breaking the unique key constraint.</p>
  
  <p>Any idea how to select EM1000?</p>