有一个排序,我自己写了一个查询:select t.bjid, t.bjmc from scwl.wl_cpbom t order by to_number(replace(t.bjid,'-',''));其查询结果如下:
ID BJID BJMC
---------------------------------
1 1 孤形扇形段底座部件三
2 1 大部件
3 1 222
4 1-1 支架
5 1-1 支架
6 1-1 锻造件
7 1-2 轴承
8 1-2 轴承
9 1-10 222
10 1-1-1 钢管01
我希望的结果入下:
ID BJID BJMC
---------------------------------
1 1 孤形扇形段底座部件三
2 1 大部件
3 1 222
4 1-1 支架
5 1-1 支架
6 1-1 锻造件
7 1-1-1 钢管01
8 1-2 轴承
9 1-2 轴承
10 1-10 222
------解决方案--------------------
分别取出1-1-1三个数字再排序
select bjid, bjid||'--' CC
from
(select '1-1' as bjid from dual
union
select '1-2' as bjid from dual
union
select '1-10-1' as bjid from dual
union
select '1-1-1' as bjid from dual)
order by
to_number(substr(CC,1,instr(CC,'-')-1)),
to_number(substr(CC,instr(CC,'-',1,1)+1,instr(CC,'-',1,2)-instr(CC,'-',1,1)-1)),
to_number(substr(CC,instr(CC,'-',1,2)+1,instr(CC,'-',1,3)-instr(CC,'-',1,2)-1))
------解决方案--------------------
写函数,假设你每个"-"分隔的小节的长度不大于3,总字符串长度小于等于150,可以用看下我写的脚本
SQL> create or replace function Fun_Digit_Replace(P_C_InStr varchar2
2 ,P_C_SepStr varchar2
3 ,P_N_LenPerSite number)
4 return varchar2 is
5
6 /*==========================================================================
7
8 功能: 填充指定字符串的每一节到指定长度,不够的在左恻用0补足,并将分隔符去除
9
10 作者: Powerise.GuoYong-Dai
11
12 说明: 此程序仅供个人学习交流,违者也没法追究
13
14 ============================================================================*/
15
16 Result varchar2(150);
17 V_N_Site1 number;
18 V_N_Site2 number;
19 V_C_InStr varchar2(150);
20 V_C_LeftStr varchar2(150);
21 V_C_RightStr varchar2(150);
22 V_C_TempStr varchar2(150);
23 begin
24
25 Result := null;
26 V_C_InStr := P_C_InStr;
27
28 if P_C_InStr is null or length(P_C_InStr) < 1 then
29 return Result;
30 end if;
31
32 if P_C_InStr is null or length(P_C_InStr) < 1 then
33 return P_C_InStr;
34 end if;
35
36 IF P_N_LenPerSite IS NULL OR P_N_LenPerSite < 1 THEN
37 return Result;
38 END IF;
39 dbms_output.put_line(V_C_InStr);
40 dbms_output.put_line(P_N_LenPerSite);
41 LOOP
42 EXIT WHEN INSTR(V_C_InStr,P_C_SepStr) < 1;
43 V_C_LeftStr := substr(V_C_InStr,1,INSTR(V_C_InStr,P_C_SepStr)-1);
44 V_C_TempStr := null;
45 select substr(V_C_InStr
46 ,INSTR(V_C_InStr,P_C_SepStr) + Length(P_C_SepStr)
47 ,Decode( INSTR(V_C_InStr,P_C_SepStr,1,2)
48 ,0
49 ,Length(V_C_InStr) + 1
50 ,INSTR(V_C_InStr,P_C_SepStr,1,2) - INSTR(V_C_InStr,P_C_SepStr) - 1
51 )
52 ) into V_C_TempStr
53 from dual;
54
55
56 V_C_RightStr := null;
57 select substr(V_C_InStr
58 ,Decode( INSTR(V_C_InStr,P_C_SepStr,1,2)
59 ,0