如何在oracle中的字母数字列中使用order by

how to use order by in alphanumeric column in oracle

在我的 table 列之一中,我的值如下所示

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

当我按此列排序时,如果该行的值高达 Y-9,则它工作正常,否则我的结果是错误的,如下所示。

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

但我想要如下输出

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

如何实现上述 result.i 我正在使用 oracle database.Any 帮助将不胜感激!!!!!

您可以通过操纵列内容并转换为数字来使用订单,例如:

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

假设数据在 table t 中,其中有一列 col 并且结构是一个字母字符串,后跟破折号,后跟数字,字母顺序和number 始终不为 NULL,则:

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

我认为好的方法是获取固定长度字段

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

它只会纠正字符串开头的两个非数字符号,直到 99999 数字