Select number
from tableName
order by Number
显示如下
1
10
11
14
14A
14AA
19
2
20
21
相反它应该显示如下
1
2
10
11
14
14A
14AA
19
20
答案 0 :(得分:3)
对于PostgreSQL和SQL Server,我假设你的专栏只包含字母数字。
MySQL& SQLite的强>
ORDER BY StrangeCol*1, StrangeCol
<强>的PostgreSQL 强>
order by cast(trim(both 'abcdefghijklmnopqrstuvwxyz'
from lower(StrangeCol)) as int), StrangeCol
SQL Server
order by 0+stuff(StrangeCol+'a',patindex('%[a-Z]%',StrangeCol+'a'),999,''), StrangeCol
<强>的Oracle 强>
order by 0+regexp_replace(StrangeCol, '[^[:digit:]]') ,StrangeCol
答案 1 :(得分:2)
ORDER BY
CAST(SUBSTRING(Number, 0,
case when patindex('%[a-zA-Z]%', Number) = 0
then len(Number)+1
else patindex('%[a-zA-Z]%', Number)
end)
as INT),
Number
答案 2 :(得分:2)
试试这个:
create table tblNum(id varchar(10))
insert into tblNum
values('1'),('10'),('11'),('14'),('14A'),('14AA'),('19'),('2'),('20'),('21')
select id from tblNum order by
CASE WHEN PATINDEX('%[a-zA-Z]%',id) > 0 then cast(left(id,(PATINDEX('%[a-zA-Z]%',id)-1)) as int) else cast(id as int) end
答案 3 :(得分:0)
您应该澄清订购的类型。例如在14AA中,你说第一部分被解释为数字,第二部分被解释为字母!!
如果你想混合它们我建议使用HEX base。