函数

本文详细介绍了SQL中常用函数的使用方法,包括字符串转换、截取子串、字符ASCII码转换、四舍五入、货币操作、日期操作等,并通过实例展示了如何正确应用这些函数。同时,文章还讲解了如何在日期比较中正确处理日期格式,以及如何使用to_char函数进行日期格式化。此外,还提供了解决空值问题的方法,确保数据处理的准确性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

n 搜索结果转换为小写

select lower(ename) from emp;  //(函数lower() 将ename搜索出来后全部转化为小写);    

n 截取子串

select substr(ename, 2, 3) from emp;    //(使用函数substr() 将搜素出来的ename字段从第二个字母开始截,一共截3个字符)   

n 字符ASC码转换

  select chr(65) from dual;  //(函数chr() 将数字转化为AscII中相对应的字符)     

select ascii('A') from dual;  //(函数ascii()与32中的chr()函数是相反的 将相应的字符转化为相应的Ascii编码)    

n 四舍五入

     select round(23.232) from dual;  //(函数round() 进行四舍五入操作)    

select round(23.232, 2) from dual;  //(四舍五入后保留的小数位数, 0 个位 -1 十位)    

         select round(23.652,1) from dual; 结果为: 23.7

         select round(23.652,-1) from dual; 20

n 货币操作

select to_char(sal, '$99,999.9999')from emp;  //(加$符号加入千位分隔符,保留四位小数,没有的补零)    

select to_char(sal, 'L99,999.9999')from emp;  //(L 将货币转化为本地币种此处将显示¥人民币)   

select sal from emp where sal>888.88 无错.但

select sal from emp where sal>$1,250,00;会出现无效字符错误. 改为:select sal from emp where sal>to_number('$1.250.00','$9,999,99');

n 日期操作

select to_char(hiredate, 'yyyy-MM-DD HH:MI:SS') from emp;  //(改变日期默认的显示格式)    

select to_char(sysdate, 'yyyy-MM-DD HH:MI:SS') from dual;  //(用12小时制显示当前的系统时间)    

select to_char(sysdate, 'yyyy-MM-DD HH24:MI:SS') from dual;  //(用24小时制显示当前的系统时间)    

   select birthdate from emp;

        显示为:

        BIRTHDATE

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

        17-12月-80

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

        改为:

        select to_char(birthdate,'YYYY-MM-DD HH:MI:SS') from emp;

        显示: 

        BIRTHDATE

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

        1980-12-17 12:00:00

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

        select to_char(sysdate,'YYYY-MM-DD HH24:MI:SS') from dual; //也可以改为:HH12

        TO_CHAR(SYSDATE,'YY

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

        2007-02-25 14:46:14

        

        to_date函数:

        select ename,birthdate from emp where birthdate > to_date('1981-2-20 12:34:56','YYYY-MM-DD HH24:MI:SS');

        如果直接写 birthdate>'1981-2-20 12:34:56'会出现格式不匹配,因为表中的格式为: DD-MM月-YY.                                                                             

n   替换空值

   select ename,sal*12+nvl(comm,0) from emp; 这样可以防止comm为空时,sal*12相加也为空的情况.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值