今天来看一下SQL中的to_char的一些常见用法吧!
一、首先介绍一下to_char中的保留小数的用法吧:
表达式to_char(值,'保留位数示范数字'),如to_char(2222.22222,'9999.99')
select to_char(222.222222,'999.99') as 值 from dual
查询结果:

但是我们在使用的时候多多少少会出现一些问题:
问题一:'保留位数示范数字'不是使用‘9’
select to_char(222.222222,'222.22') as 值 from dual
这样查询会报错:因为2不是字符格式的关键字,必须是9

问题二:小数点位数前保留位数不足
select to_char(222.222222,'99.99') as 值 from dual
上面的sql小数点前面有三位数,但是你的小数前面的保留数值只有两位值就会引发问题,查询出来的结果不是你想要的
查询结果:

注意这并不是你装的oracle,会查询客户端出现问题,也不是乱码。
问题三:to_char中的第二个参数的小数点后面位数高于第一个参数,会用0补齐
select to_char(222.22,'999.999999') as 值 from dual
查询结果:

问题四:to_char中的第二个参数的小数点前面位数高于第一个参数,会用空格补齐
正常:select to_char(222.22,'999.99') as 值 from dual
高于:select to_char(222.22,'99999999999999999999999.99') as 值 from dual
查询结果如图:

从图中可以很明显的看出来高于的查询语句数值前面多出很多空格
二、介绍一下连接字符‘||’的使用
常见:百分号,单位符号,价钱等等的连接
1.百分号
select 22.22||'%' as 值 from dual
查询结果:

2.价钱
select '¥'||22.22 as 值 from dual
查询结果:

在大型的sql中最好少用‘||’,应为会影响性能
网友评论