How to use order in oracle alphanumeric column

In my table, one of the columns I have a value similar to below

Y-1
Y-2
Y-3
Y-4
Y-5
Y-6
Y-7
Y-8
Y-9
Y-10
Y-11
Y-12
Y-13
Y-14

when I order my work on this column, if the row has a value up to Y-9, then the other result is not as shown below.

Y-1
Y-10
Y-11
Y-12
Y-13
Y-14
Y-2
Y-3
Y-4
Y-5
Y-6
Y-7
Y-8
Y-9

But I want a way out as shown below

Y-1
Y-2
Y-3
Y-4
Y-5
Y-6
Y-7
Y-8
Y-9
Y-10
Y-11
Y-12
Y-13
Y-14

How to achieve the result above. I am using oracle database. Any help would be greatly appreciated !!!!!

+4
source share
3 answers

You can use order by manipulating the contents of a column and pointing to a number, for example:

 order by substr(col1, 1,2), TO_NUMBER(sustr(col1, 3,10))
+1
source

, t col, - , , , , NULL, :

select col from t
order by substr(col, 1, instr(col, '-')), to_number(substr(col, instr(col, '-')+1))
+2

I think a good way is to get a field of constant length

select col from t
order by substr(col, 1, 2)|| lpad(substr(col, 3),5,'0')

it will fix the work with only two non-digital characters at the beginning of the line to the number 99999

0
source

Source: https://habr.com/ru/post/1659404/


All Articles