对字母数字混合的数据排序

在SQLCookBook有一例子,将混合数据按数字或者字母进行排序:

例子:

1.新建一个视图V

create view V as select ename|' '|empno as data from emp;

2.显示数据

select * from V

 

DATA

---------------------------------------------------

SMITH 20

ALLEN 30

WARD 30

JONES 20

MARTIN 30

BLAKE 30

CLARK 10

SCOTT 20

KING 10

TURNER 30

ADAMS 20

JAMES 30

FORD 20

MILLER 10

14 rows selected

 

书中实现的方法如下:

按照empno对混合数据进行排序

select data from V order by replace(data,replace(translate(data,'0123456789','##########'),'#',''),'')

数据显示如下:

 

DATA

---------------------------------------------------

CLARK 10

KING 10

MILLER 10

JONES 20

FORD 20

ADAMS 20

SMITH 20

SCOTT 20

WARD 30

TURNER 30

ALLEN 30

JAMES 30

BLAKE 30

MARTIN 30

14 rows selected

 

其实,也可结合instr、substr函数来实现相同的功能,结果一致

select data from V order by substr(data,instr(data,' '),length(data))

 

DATA

---------------------------------------------------

CLARK 10

KING 10

MILLER 10

JONES 20

FORD 20

ADAMS 20

SMITH 20

SCOTT 20

WARD 30

TURNER 30

ALLEN 30

JAMES 30

BLAKE 30

MARTIN 30

14 rows selected

 

同理,按照ename排序也一样,这里不再重复。

 

你可能感兴趣的:(Oracle开发)