oracle字符串数字字段排序超过10排序问题之cast函数的用法

#前言
项目中发现有一个字符串字段在排序的时候不能按照从大到小排列。原因主要是由于排序时按照字符串的顺序进行排序(本人也是被字段中保存的全是数字给迷惑了,以为是数字类型了)。

#解决方法
select emp from tab order by cast(emp as int) as emp desc;

##扩展
语法:cast( 需要转换的字段 as 数据类型 )
cast函数能够转换的类型有如下几种:
1.varchar2() 样例 select emp from tab order by cast(emp as varchar2(10)) as emp desc;
2.int 样例 select emp from tab order by cast(emp as int) as emp desc;
注意:如果是小数按照四舍五入进行取值。
select cast(‘66.4’ as int) as result from tab ; result:66
select cast(‘66.6’ as int) as result from tab ; result:67
3.decimal(m,n) m:表示值总位数,n:精确到小数点后位数
样例 select emp from tab order by cast(emp as decimal(m,n)) as emp desc;
实例 select cast(‘666.4468’ as decimal(5,2)) as result from tab ; result:666.45

你可能感兴趣的:(数据库,oracle)