Oracle数据库学习---其他函数及转换函数

转换函数 to_number to_char to_date
Oracle的类型转换分为自动类型转换和强制类型转换,建议使用显示转换函数
**char-->number 使用 to_number('字符数字')**
select to_number('123') from dual
select '17-12月-88'+30 from dual
select * from emp where empno='7369'
**number-->char 使用  to_char(数字)**
select sal, to_char(sal) from emp
查看81年10月份后入职的员工信息
**char-->date 前提是格式要正确 日-月-年**
select * from emp where hiredate >1981/10/12;
select * from emp where hiredate >'1981/10/12';
select * from emp where hiredate >'12-10月-1981';
**date-->char 转换后是默认的格式**
select ename,job, hiredate,to_char(hiredate) from emp
**转换格式 number->>char to_char(数字,'格式') 格式有:$999,999.999  L000,000.000**
select ename,sal, to_char(sal,'$9999.999999') from emp
select ename,sal, to_char(sal,'L00000000.0000') from emp
**转换格式 char->>date   转换的时候需要加上格式说明,否则使用默认格式的字符串类类型 dd-mm-yyyy**
select to_date('1988-10-17','yyyy/mm/dd') from dual;
**转换格式 date->>char 转换的时候加上格式说明,否则使用默认格式  dd-mm-yyyy**
select ename,job,hiredate,to_char(hiredate,'yyyy-mm-dd') from emp;
select ename,job,hiredate,to_char(hiredate,'yyyy/mm/dd') from emp;
select ename,job,hiredate,to_char(hiredate,'yyyy"年"mm"月"dd') from emp;
查看81年10月份后入职的员工信息
select * from emp where hiredate>1981/12/17
--解决方式一  自动类型转换 char->>date
select * from emp where hiredate>'17-12月-1981'
--解决方式二  to_date(字符日期,格式)
select * from emp where hiredate > to_date('1981/12/17','yyyy/mm/dd')
--解决方式三  to_char(date,格式) 注意:一定加格式
select * from emp where to_char(hiredate) > '1981/12/17';
select * from emp where to_char(hiredate,'yyyy/mm/dd') > '1981/12/17'
其他函数 nvl nvl2 decode
查看员工当月的实发工资
select ename,job,sal,comm from emp;
select ename,job,sal,sal+comm 实发工资 from emp
**使用nvl:将空值Null替换为指定的缺省值,适用于字符、数字、日期等类型**
**NVL(exp1,exp2):如果表达式exp1为null,则返回exp2的值,否则傅返回exp1的值**
select ename,job,sal,comm,sal+nvl(comm,0) from emp;--单条件判断 if()
**使用nvl2()**
**NVL2(EXP1, EXP2, EXP3)如果exp1的值为null,则返回exp2的值,否则返回exp3的值**
select ename,job,sal,comm,nvl2(comm,sal+comm,sal) from emp;---if() else
**使用decode**
**DECODE(VALUE,IF1,THEN1,IF2,THEN2,……,ELSE)** 
**如果value的值为if1,则返回then1的值,如果value的值为if2,则返回then2的值,……,否则返回else值**
select * from emp
select ename ,sal,comm,decode(job,'MANAGER','经理','SALESMAN','销售','普通员工') from emp--多条件判断 if()else If() else if     
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值