【问题标题】:Sort String column which has numbers and Alphabets( Oracle SQL)排序具有数字和字母的字符串列(Oracle SQL)
【发布时间】:2018-08-23 14:38:56
【问题描述】:

我想对可以包含数字和字母的字符串列进行排序。

SQL 脚本:

select distinct  a.UoA, b.rating , b.tot from omt_source a left join  
wlm_progress_Scored b
on a.UoA = b.UoA 
where a.UoA in (select UoA from UserAccess_dev
where trim(App_User) = lower(:APP_USER))
order by 
  regexp_substr(UoA, '^\D*') ,
  to_number(regexp_substr(UoA, '\d+'))--);

我目前得到的输出:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
23 
26B
26A
27 
28 
30 
31 
32 
33 
34B
34A

但是,我希望 2634 按此顺序排列

26A
26B
34A
34B

任何建议都会很有帮助 谢谢

【问题讨论】:

    标签: sql oracle sql-order-by natural-sort


    【解决方案1】:

    如果您的第一个 order by 子句确保主要排序顺序基于 UoA 字段的数字部分,那么您的第二个 order 子句实际上可能只是 UoA 字段本身。即

        order by 
          regexp_substr(UoA, '^\D*'), UoA;
    

    【讨论】:

    • 非常感谢,我刚刚使用了这个并让它工作了----- order by regexp_substr(UoA, '^\D*') nulls first, to_number(regexp_substr(UoA, '\d+') ),--);, regexp_replace(UoA, '^\D*\d+') NULLS FIRST;
    • @Praveen。 . .如果这解决了你的问题,你应该接受它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-07
    • 2019-02-17
    • 1970-01-01
    • 2014-02-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多