Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>For your example this would suffice (Oracle syntax):</p> <pre><code>ORDER BY SUBSTR(name,4) </code></pre> <p>If the number of characters before the first hyphen can vary, you can do this (again Oracle syntax):</p> <pre><code>ORDER BY SUBSTR(name,INSTR(name,'-')+1) </code></pre> <p>However that won't work if you have codes like:</p> <pre><code>AAA-10-1 AAA-8-1 AAA-9-1 </code></pre> <p>and expect <code>AAA-10-1</code> to appear after <code>AAA-9-1</code>. Then you will need to parse it further:</p> <pre><code>ORDER BY LPAD(SUBSTR(name,INSTR(name,'-')+1, INSTR(name,'-',1,2)-INSTR(name,'-')-1),10,'0'), LPAD(SUBSTR(name,INSTR(name,'-',1,2)+1),10,'0') </code></pre> <p>(NB I have used LPAD(x,10,'0') to turn a value like '1' into '0000000001' and so on, rather than use TO_NUMBER since this could fail if there are any non-numerics in your data.)</p> <p>Example:</p> <pre><code>with data as ( select 'AAA-1' name from dual union all select 'PQR-1-4' name from dual union all select 'PQR-1-3' name from dual union all select 'AAA-10-10' name from dual union all select 'AAA-10-1' name from dual union all select 'AAA-9-10' name from dual union all select 'AAA-9-1' name from dual ) select * from data ORDER BY LPAD(SUBSTR(name,INSTR(name,'-')+1, INSTR(name,'-',1,2)-INSTR(name,'-')-1),10,'0'), LPAD(SUBSTR(name,INSTR(name,'-',1,2)+1),10,'0'); </code></pre> <p>Output:</p> <pre><code>NAME --------- PQR-1-3 PQR-1-4 AAA-9-1 AAA-9-10 AAA-10-1 AAA-10-10 AAA-1 </code></pre> <p>And if AAA-1 should come first:</p> <pre><code>ORDER BY LPAD(SUBSTR(name,INSTR(name,'-')+1, INSTR(name||'-','-',1,2)-INSTR(name,'-')-1),10,'0'), LPAD(SUBSTR(name,INSTR(name||'-','-',1,2)+1),10,'0') nulls first </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload