数据库教程:oracle教程之oracle的单行函数详解和举例

字符函数 –大小写控制函数 –upper select * from emp where job = upper('salesman'); –


字符函数

  --大小写控制函数  --upper  select * from emp where job = upper('salesman');    --lower  select * from emp where lower(job) = 'clerk';    --initcap  select empno, initcap(ename) from emp;    --upper、lower、initcap这三个函数的共同点,如果输入参数值为null时,则返回null  select empno, initcap(null) from emp;    --字符控制函数    --字符串连接符,实现雇员名与工资两列的连接  select ename || ':' || sal from emp;    --字符串连接的函数concat,实现雇员名与工资两列的连接  select concat(concat(ename,':'),sal) from emp;    --截串函数 substr  select * from emp where substr(job,1,4) = 'SALE';    --求字符串长度的函数 length  select * from emp where length(ename) = 6;    --instr 求子串在字符串中的位置  select instr('hello oracle','oracle') from dual;    select instr('hello oracle hello oracle', 'oracle', 1, 2) from dual;    --左填充函数 lpad  select lpad(job,9,'*') from emp;    --右填充函数 rpad  select rpad(job, 9, '*') from emp;    --替换函数 replace  select replace('hello oracle','oracle','world') from dual;  

数值函数

  --四舍五入的函数 round    --求员工的日工资  select round(sal/30,2) from emp;      --截断函数 trunc  select round(sal/30,2),trunc(sal/30,2) from emp;      --求余数的函数 Mod    --求员工号为偶数的员工信息  select * from emp where mod(empno,2)=0;  

日期和时间函数

  --sysdate  select sysdate-1 昨天 , sysdate 今天, sysdate+1 明天 from dual;    --months_between  select round(months_between(sysdate,hiredate)/12)from emp;    --add_months  select ename,add_months(hiredate,30*12) from emp;    --next_day  select next_day(sysdate,'星期一')from dual;    --last_day  select sysdate,last_day(sysdate) from dual;  select empno,ename,last_day(hiredate) from emp;    --round  select hiredate, round(hiredate,'YEAR'), round(hiredate,'MONTH') from emp where empno=7654;    --trunc  select hiredate, trunc(hiredate,'YEAR'), trunc(hiredate,'MONTH') from emp where empno=7654;

转换函数

  --隐式数据类型转换的举例  select * from emp where sal>'2000';  select * from emp where hiredate='02-4月-81';    --to_char 日期类型转换成字符类型  select to_char(hiredate,'YYYY-MM-DD') from emp;  select to_char(hiredate,'YYYY"年"MM"月"DD"日"') from emp;  select to_char(hiredate,'DD-MON-RR','NLS_DATE_LANGUAGE=AMERICAN') from emp;    --to_char 数值类型转换成字符类型  select sal,to_char(sal,'L0,000,000.00')from emp;  select sal,to_char(sal,'L9,999,999.99')from emp;  select sal,to_char(sal,'$9,999,999.99')from emp;    --to_date  字符类型转换成日期类型  select ename,hiredate from emp where hiredate>to_date('1981-12-31','YYYY-MM-DD');    --to_number  字符类型转换成数值类型  select ename,sal from emp where sal>to_number('¥2000','L99999');

通用函数

  --nvl  select ename,sal,comm,sal+nvl(comm,0) from emp;    --nvl2  select ename,sal,comm,nvl2(comm,comm+sal,sal) from emp;    --nullif  select empno,ename,hiredate,nullif(hiredate,trunc(sysdate,'MONTH'))from emp;    --coalesce  select ename,sal,comm,coalesce(sal+comm,sal) from emp;

条件表达式

  --想显示全部雇员的职位,但是这些职位要求替换为中文显示:  --  --CLERK:办事员;  --SALESMAN:销售;  --MANAGER:经理;  --ANALYST:分析员;  --PRESIDENT:总裁;      --case表达式  select empno,ename,  case job  when 'CLERK' then '办事员'  when 'SALESMAN' then '销售'  when 'MANAGER' then '经理'  when 'ANALYST' then '分析员'  else '总裁'  end   from emp;      --decode函数  select empno,ename,job,decode(job,'CLERK','办事员','SALESMAN','销售','MANAGER','经理','ANALYST','分析员','总裁')from emp;      --case表达式,区间值的判断  select empno,ename,sal,  case when sal<2000 then '低'  when sal<5000 then '中'  else '高'  end  from emp;

嵌套函数

  --参照雇员信息表,想显示距聘用日期3个月后的下一个星期一的日期,且日期格式如:2017-01-06。    select empno,ename,to_char(next_day(add_months(hiredate,3),'星期一'),'YYYY-MM-DD') new_date   from emp;        --参照雇员信息表,显示雇员日薪并四舍五入到2 位小数的结果,然后对薪资格式以‘¥ 1,182.19’这样的例子形式进行格式化    select empno,ename,sal,to_char(round(sal/30,2),'L9,999.99')  from emp;  

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/dtteaching/801262.html

(0)
上一篇 2021年9月13日
下一篇 2021年9月13日

精彩推荐