我是oracle的新手,我遇到了问题.我有一个名为file_id的列.
当我按顺序排序时,它会对字符串进行排序
1
1
10
100
11
11
110
114
12
300
31
4200
B14
B170
B18
Run Code Online (Sandbox Code Playgroud)
编辑:我希望它以这种方式排序.
1
1
10
11
11
12
31
100
300
4200
B14
B18
B170
Run Code Online (Sandbox Code Playgroud)
以下答案非常有效.我现在遇到的其他问题......我的记录是空白的.我怎么能在最后制作空白唱片?
1
1
10
11
11
12
31
100
300
4200
BLANK
BLANK
BLANK
BLANK
BLANK
B14
B18
B170
Run Code Online (Sandbox Code Playgroud)
谢谢您的帮助.
Ego*_*off 26
select column
from table
order by
regexp_substr(column, '^\D*') nulls first,
to_number(regexp_substr(column, '\d+'))
Run Code Online (Sandbox Code Playgroud)
Nor*_*oor 10
这是一个老问题,但它是谷歌的第一个热门,所以我想我会分享另一种解决方案:
select column
from table
order by
LPAD(column, 10)
Run Code Online (Sandbox Code Playgroud)
LPAD函数用空格填充字符串的左侧,以便以数字方式对结果进行排序.这适用于非数字值,空值将最后排序.如果您知道要排序的字符串的最大长度(您可能需要调整第二个参数以满足您的需要),这很有效.
资料来源:http://www.techonthenet.com/oracle/questions/sort1.php
编辑:
我注意到虽然我的解决方案适合我的情况,但输出与接受的答案略有不同(http://www.sqlfiddle.com/#!4/d935b8/2/0):
1
1
10
11
11
12
31
100
110
114
300
A14
A18
4200
A170
(null)
(null)
Run Code Online (Sandbox Code Playgroud)
4200应该在300之后.对于我的情况,这已经足够了,但情况可能并非总是如此.