您的位置:首页 > 数据库 > Oracle

oracle学习笔记单行函数

2017-09-23 11:39 323 查看
单行函数
只对一行进行变换 每行返回一个结果
单行函数分 字符、数值、日期、转换、通用

字符函数:大小写控制函数、字符控制函数

大小写控制函数:lower, upper, initcap
字符控制函数:concat,substr,length,instr,lpad|rpad,trim,replace
lower,upper,initcap
select lower('SQL') from dual;
--结果 sql
select upper('sql') from dual;
--结果 SQL
select initcap('SQL COurs') from dual;
--结果 Sql Cours 首字母大写
concat,substr,length,instr,lapd|rpd,trim ,replace
select concat('hello','world') from dual; //结果 helloworld
select substr('HelloWorld',1,4) from dual; //结果 Hell  从第一个字符开始取4个字符
select length('hellowrld') from dual; //结果 9 求字符长度*/
select instr('Helloword','w') from dual; //结果 6 第一次出现W的位置
select lpad(salary ,10,'&') from employees ; //结果 &&&&&&2600 左填充&
select rpad(salary ,10,'&') from employees ; //结果 2600&&&&&& 左填充&
select trim('H' from 'HHllWoHldHH')from dual; //结果 llWoHld 去首尾不去中间
数字控制 round,trunc,mod
select round(45.36954,4) from dual; //  45.3695四舍五入
select trunc(45.36954,3) from dual; //  45.369 截断
select mod(1600,300) from dual;     //   100 求余数
日期控制 日期只可加减 months_betwwen,add_months,next_day,last_day

两个日期相减返回日期之间相差的天数
可以用数字除24来向日期中加上或减去天数
--查询公司中入职时间是每个月最后两天的员工
select last_name,to_char(hire_date,'yyyy-mm-dd') hdate
from employees
where hire_date=last_day(hire_date)-1
--查询到2005年入职超过5年的员工
select last_name,to_char(hire_date,'yyyy-mm-dd') from employees
where to_date('2005-12-31','yyyy-mm-dd')-hire_date >=5
下个月的今天(系统时间上加1个月)
select add_months(sysdate,1) from dual;
--两天后的日期
select next_day(sysdate,2) from dual;
转换 to_char,to_date,to_number
--隐式转换
select '12'+3 from dual;   //char自动转换为number 加减
select sysdate +2 from dual; //number 自动转换为date
--显式转换
select last_name, to_char(hire_date,'yyyy-mm-dd') hire_date from employees;
select to_char(12345678.123,'999,999,999.99') from dual; // 12,345,678.12
select to_char(12345678.123,'000,000,999.99') from dual; // 012,345,678.12 没有的们用0填充
select to_char(12345678.123,'L999,999,999.99') from dual; // $12,345,678.12 'L'为当地货币
select to_number('$12,345,678.12','L999,999,999.99') from dual; // 12345678.12
select to_number('12,345,678.12','999,999,999.99') from dual;  // 12345678.12
通用函数 这些函数适用于任何数据类型,同时也适用于空值
nvl(expr1,edpr2),nvl2(expr1,expr2,expr3),nullif(expr1,expr2),coalesce(expr1……exprn)
nvl 将空值转换成一个已知的值 可用于日期、字符、数字

--求公司员工的年薪(含commission_pct)commisson_pct列中有空值
select last_name,salary*12*(1+nvl(commission_pct,0)) "nianxin" from employees;

--输出last_name,department_id,当department_id为null时,显示‘没有部门’
select last_name, nvl(to_char(department_id,'9999'),'没有部门') Dep from employees;
select last_name, nvl(to_char(department_id),'没有部门') Dep from employees; //简写
--NVL中的“没有部门” 是char类型 要把department_id显式转换成为NUMBER 使()中的数据类型一至
nvl2 (expr1,expr2,expr3) 当expr1不为null 返回expr2 ,为null返回expr3
--查询员工的奖金率,若为空,返回0.01,若不为空,返回实际奖金率+0.015
select last_name,commission_pct ,nvl2(commission_pct,commission_pct + 0.015 ,0.01)
from employees;
nullif (expr1,expr2) 两个表达式相等返回NULL 不相等返回表达式1 expr1
select first_name,length(first_name) "expr1",
last_name,length(last_name) "expr2",
nullif(length(first_name),length(last_name)) result
from employees;
case 表达式

CASEexprWHENcomparison_expr1THENreturn_expr1 [WHEN comparison_expr2 THEN return_expr2 WHENcomparison_exprnTHENreturn_exprn ELSEelse_expr]END
--查询部门号为10,20, 30 的员工信息, 若部门号为
--10 则打印其工资的1.1 倍,
--20 号部门, 则打印其工资的1.2 倍,
--30 号部门打印其工资的1.3 倍数
select last_name,department_id,case department_id when 10 then salary * 1.1
when 20 then salary * 1.2
else salary * 1.3 end  new_salary
from employees
where department_id in (10,20,30)

--上面的加显示其他的人的工资
select last_name,department_id,case department_id when 10 then salary * 1.1
when 20 then salary * 1.2
when 30 then salary * 1.3
else salary  end  new_salary
from employees
decode
DECODE(col|expression, search1, result1 , [, search2, result2,...,] [, default]) ------ 用小括号代替 when ……then
--上面一样的题
select last_name,department_id,decode(department_id,10,salary * 1.1,
20,salary * 1.2,
salary * 1.3) new_salary
from employees
where department_id In (10,20,30)
--加显其他员工工资
select last_name,department_id,decode(department_id,10,salary * 1.1,
20,salary * 1.2,
30,salary * 1.3,
salary) new_salary
from employees
下载本文用数据库文件http://down.51cto.com/data/2336926
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息