一个字段存放WBS编号,如何简单排序?
2014-10-11 11:29
288 查看
字段内容为
1
1.1
1.1.1
1.1.2
1.2
...
1.10
1.10.1
...
2
2.1
...
如何排序呢?
order by后1.10等就会排在1.2的前面,怎么办呢?
解决方案:写个函数,把这个字符串换算成一个数字,然后排序。
比如
1=1*1000,
1.1=1*1000+1*100
1.10=1*1000+10*100
1.2=1*1000+2*100
order by function(字段)
具体实现:
create or replace function mywbs_sort(wbs in varchar2) return varchar2 is
Result varchar2(20);
restwbs varchar2(20);
pos int(32);
str varchar(2);
i int(32);
m int(32);
begin
Result := 0;
i := 1;
restwbs := wbs;
while length(restwbs) > 0 loop
pos := instr(restwbs, '.', 1);
if pos = 0 then
str := restwbs;
pos:=100; --为了让下一次restwbs取空,循环结束条件
else
str := substr(restwbs, 1, pos - 1);
end if;
if i = 1 then
m := 10000;
end if;
if i = 2 then
m := 1000;
end if;
if i = 3 then
m := 100;
end if;
if i = 4 then
m := 10;
end if;
Result := Result + to_number(str) * m;
restwbs := substr(restwbs, pos + 1); -- 默认截取到字符串的结尾
i := i + 1;
end loop;
return(Result);
end mywbs_sort;
1
1.1
1.1.1
1.1.2
1.2
...
1.10
1.10.1
...
2
2.1
...
如何排序呢?
order by后1.10等就会排在1.2的前面,怎么办呢?
解决方案:写个函数,把这个字符串换算成一个数字,然后排序。
比如
1=1*1000,
1.1=1*1000+1*100
1.10=1*1000+10*100
1.2=1*1000+2*100
order by function(字段)
具体实现:
create or replace function mywbs_sort(wbs in varchar2) return varchar2 is
Result varchar2(20);
restwbs varchar2(20);
pos int(32);
str varchar(2);
i int(32);
m int(32);
begin
Result := 0;
i := 1;
restwbs := wbs;
while length(restwbs) > 0 loop
pos := instr(restwbs, '.', 1);
if pos = 0 then
str := restwbs;
pos:=100; --为了让下一次restwbs取空,循环结束条件
else
str := substr(restwbs, 1, pos - 1);
end if;
if i = 1 then
m := 10000;
end if;
if i = 2 then
m := 1000;
end if;
if i = 3 then
m := 100;
end if;
if i = 4 then
m := 10;
end if;
Result := Result + to_number(str) * m;
restwbs := substr(restwbs, pos + 1); -- 默认截取到字符串的结尾
i := i + 1;
end loop;
return(Result);
end mywbs_sort;
相关文章推荐
- java中list里面存放map,根据map中的某一个字段进行排序
- java中list里面存放map,根据map中的某一个字段进行排序
- 一条数据更新的语句,如何将排序的序号写入本表中的一个字段中
- java中list里面存放map,根据map中的某一个字段进行排序
- mysql如何把查询到的数据加上一个自动编号字段
- java中list里面存放map,根据map中的某一个字段进行排序 lambda
- 如何取得一个表的所有字段名用逗号分割
- 如何实现一个简单的remoteing实例
- 触发器(当2个表中的相应值改变时同时改变一个表中的一个字段)(同时有处理“无法解决 equal to 操作的排序规则冲突”问题)
- 一个关于nvarchar字段排序,中英文混合排序的问题
- 如何创建一个简单的JAVA存储过程?[转贴]
- 如何判断一个已知的表中是否存在某个字段,如果不存在就创建它.
- 简单却常用的一个SQL功能:1句SQL语句返回多个字段值到指定变量
- [愚翁专栏]如何用C#写一个简单的Login窗口
- 举一个简单例子来说明:如何在JSP中使用JavaBean
- 如何用C#写一个简单的Login窗口
- vb与sql数据库下下给access创建一个有自动编号字段的表
- 如何写一个最简单的WebPart
- 如何在DataGrid前加一个列让其id按顺序排列,而非绑定的id字段的乱七八糟的排序???
- 如何显示一个表的字段信息(ACCESS)