Oracle单行函数和多行函数实例

上一篇 / 下一篇  2012-04-11 09:34:46 / 个人分类:数据库

单行函数和多行函数示意图:

(t/e0jO8y5U^l(K`0

  单行函数分为五种类型:字符函数、数值函数、日期函数、转换函数、通用函数51Testing软件测试网&^ X#|:o}m

cMa;Z n0

  单行函数:

7Sp\SS!j2vJ:rT0

51Testing软件测试网 T'Mq_*n$s\4z

51Testing软件测试网1W |*@xj5o1J \

--大小写控制函数51Testing软件测试网2Ng%rP1[H
select lower('Hello World') 转小写, upper('Hello World') 转大写 from dual;51Testing软件测试网%g5Gmk*Gg1l'@m#PM
--initcap: 首字母大写51Testing软件测试网 l7WG jU$c:P
select initcap('hello world') 首字符大写 from dual;51Testing软件测试网v/lW"}p6Ud? \*\

z M%w&a C F?^2?8^O0--字符控制函数
K'z:X OoiG4n l7C0-- concat: 字符连接函数, 等同于  ||
R5}"_-u)r X&qOr0select concat('Hello',' World') from dual;51Testing软件测试网U$u A4yknQ4s
--substr:求母串中的某个子串51Testing软件测试网(GK8pgGizO
select substr('Hello World',3) from dual;
3AK~R9M0select substr('Hello World',3,4) from dual;
eu,~ XjRs0--length和lengthb: 字符数和字节数
#H%E {(~(g0select length('China') 字符数, lengthb('China') 字节数  from dual;51Testing软件测试网 pC,Vyh*?
--instr:在母串中,查找子串的位置
w4tg~8u5d e r_0select instr('Hello World','ll') from dual;
W$D1] gzj h;W0--lpad,rpad: 左右填充,将abcd用*填充到10位51Testing软件测试网,g;t?,^R6r1y*{Ei
select lpad('abcd',10,'*') 左填充, rpad('abcd',10,'*') 右填充 from dual;51Testing软件测试网h-p N(mlB;u"{
--trim: 去掉字符串前后指定的字符51Testing软件测试网7S3I5vph l&M
select trim('H' from 'Hello WorldH') from dual;
'Mh BT,BC0--replace:字符串替换函数51Testing软件测试网J'e _I!N"VO P
select replace('Hello Wordl','l','*') from dual;

7LJ{KO!I h)`0

V Qe&o)P0--数字函数51Testing软件测试网G:g nv5L-B m Zq
select round(45.926,2) 四舍五入, trunc(45.926,2)  截断 ,mod(1600,300) 求于 from dual;51Testing软件测试网:GmQjAHX3O
--ROUND函数51Testing软件测试网mMJ*a+U9T5\
select round(45.923,0) 整数位, round(45.923,-1) 十位,round(45.923,-2) 百位 from dual;51Testing软件测试网2oc{{)Z7S~5bo

51Testing软件测试网H]%N)`)y

--日期函数51Testing软件测试网_G/{%wq/Pqs0Q
--显示当前日期
({ M/F&[{x3M/M0select sysdate from dual;51Testing软件测试网q0[V"qQOsp
--显示时间部分51Testing软件测试网L$}?4Q7Yw7F
select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') from dual;51Testing软件测试网x5D*qY&P@ a&]
--显示昨天,今天和明天,加减数字仍未日期51Testing软件测试网k d l+crD
select sysdate-1 昨天, sysdate 今天, sysdate+1 明天 from dual;
n9}/\e _ v W%Q_s0--两个日期相减,结果为相差的天数,查询员工信息,显示员工工龄。两个日期不能相加
y E ^U8d'kH0select empno,ename, sysdate-hiredate 天 from emp;
BPl@D;Z-p0--查询员工信息,显示员工工龄,分别按照天,星期,月显示
4eI-zF6Cr xI0select empno,ename,sysdate-hiredate 天,(sysdate-hiredate)/7 星期, (sysdate-hiredate)/30 月 from emp;
D*z;B U zKU;D `;l5M0--months_between:两个日期相差的月数51Testing软件测试网7H)N`kr1K n
select (sysdate-hiredate)/30 方式一, months_between(sysdate,hiredate) 方式二 from emp;51Testing软件测试网n m5QF.Y$tk*w
--add_months:在指定日期上加上若干个月
1[w gb-M3w0select add_months(sysdate,1)  下个月, add_months(sysdate,123) "123个月后" from dual51Testing软件测试网t(h]#x:BSJ
--last_day: 某个日期当月的最后一天51Testing软件测试网;^2r;Za d(t
select last_day(sysdate) from dual;
"uw? H` W'bS&p0--next_day:下周六
z#y[]]G9]0select next_day(sysdate,'星期五') from dual;
)D,BoN {Np0--对日期进行四舍五入51Testing软件测试网I8FB#CwcJtSC
select round(sysdate,'MONTH')  月,round(sysdate,'YEAR') from dual; 51Testing软件测试网%K#Jx[4v/N,v
--对日期进行截断51Testing软件测试网1h)x{3V@x}2K-xo
select trunc(sysdate,'MONTH')  月,trunc(sysdate,'YEAR') from dual;
?E^dQ0--日期格式51Testing软件测试网 R6sw$[aJ
select * from emp where hiredate=to_date('1982-01-23','yyyy-mm-dd');51Testing软件测试网:\;Yuz;i
-- 查询当前日期:显示:  2011-09-17 15:12:15今天是星期六51Testing软件测试网H/H"U0Xoahps
select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss"今天是"day') from dual;
;@E5_.B-r^W0--查询员工信息,显示员工的编号,姓名,月薪,要求有货币代码(L),千位符(,),小数点(.),51Testing软件测试网4~C Hc1~-w]
select empno,ename,to_char(sal,'L9,999.99') from emp;51Testing软件测试网BFG2g&Z#[

51Testing软件测试网w^ BW5?

51Testing软件测试网/B.G!Cd U"Uj5M*y

--通用函数51Testing软件测试网 ?0p$]'p.?7T q
--nvl(exp1,exp2):当exp1为空时,返回exp251Testing软件测试网_/k#f u%E4|'_
--nvl2(exp1,exp2,exp3):当exp1为空时,返回exp3;否则返回exp251Testing软件测试网 ] k3o1UqI5| i8]/r
select ename,sal*12+nvl2(comm,comm,0) 年收入 from emp;51Testing软件测试网/H%`$} i8T1p
--NULLIF (expr1, expr2),如果expr1=expr2,返回null;否则,返回expr1
0d;F'qL1aH0select nullif('abc','abc') from dual;
4yEQN;^%}0select nullif('abc','abcaa') from dual;51Testing软件测试网-O1QEY~3O1lI!r7v
--COALESCE :找到参数列表中,第一个不为空的值51Testing软件测试网-\!\X$Y;FY#Z
select ename,comm,sal,COALESCE(comm,sal) from emp;51Testing软件测试网bz,y9bIGL
--给员工涨工资,根据职位涨,总裁涨1000,经理涨600 其他人员涨400
1{1?3t tS [(zP+b$JfY0select ename,job,sal 涨前工资, case job when 'PRESIDENT' then sal+1000
R'W_7S(i0f\;ut0                                         when 'MANAGER'   then sal+600
*K&MIa%zHCK7F0                                         else sal+40051Testing软件测试网$w*^|2k`
                                 end 涨后工资51Testing软件测试网YE \9K1RA v
from emp;51Testing软件测试网_ m btc#dK

51Testing软件测试网B*x6U eGcm

select ename,job,sal 涨前工资, decode(job,'PRESIDENT',sal+1000,
1s^,`vU3yh"c o0                                            'MANAGER',sal+600,51Testing软件测试网KJDnb8e PD|_r3Z
                                             sal+400) 涨后工资
EW(J3oY R]0from emp;51Testing软件测试网MU w\U5v_\]

51Testing软件测试网6V*B%DU5u3f(i

51Testing软件测试网8[:h9U[g L

  多行函数51Testing软件测试网t$_h(Qe"w

`N3HoGM0  和单行函数相比,oracle提供了丰富的基于组的,多行的函数。这些函数能在select或select的having子句中使用,当用于select子串时常常都和GROUP BY一起使用。多行函数分为接收多个输入,返回一个输出。

:n5Y!RF0A*e051Testing软件测试网xD{:p7B&D V

  组函数:

4^ qY:d l|3D0

zh.};SZ#?g.qL9z051Testing软件测试网f_ }Q$M

m2Aq1?4V2P0

--求员工的工资总和
P)a;Id5__ Qs0select sum(sal) from emp;51Testing软件测试网Q-g8\_4fFS
--求个数51Testing软件测试网 cP-yfpC&g
select count(*) from emp;51Testing软件测试网p&U hV,s4D#{K`*rt)J
--求平均工资51Testing软件测试网"R8M2K!B2L\n;Of
select sum(sal)/count(*) 方式一, avg(sal) 方式二 from emp;51Testing软件测试网 Wi-~%^'ZN0e
--关于空值:组函数会自动滤空
gK Lq4\Z0select count(*), count(comm) from emp;
IDoRz(Y x5J6w]0--max和min:求最高工资和最低工资
!H&_.C$E.K0select max(sal) 最高工资,min(sal) 最低工资 from emp;
b9]+U[]0--分组数据:求各个部门的平均工资
4W?M L2B#o h&A0select deptno,avg(sal) from emp group by deptno;51Testing软件测试网}5ZN*W9e;kc
--group by作用于多列: 按部门,不同的工种,统计平均工资51Testing软件测试网Ri!l/}vj
--group by作用于多列:先按照第一列分组;如果相同,再按照第二列分组51Testing软件测试网&q!XV,U,eIf
select deptno,job,avg(sal) from emp group by deptno,job;
nHU0a~!O2xG&c0--:求部门的平均工资大于2000的部门
-N` C7Y eVhf0select deptno,avg(sal) from emp group by deptno having avg(sal)>2000;51Testing软件测试网)mzTNNX$XU
--group by的增强
#Xm&C6H|kz!Z)D0select deptno,job,sum(sal) from emp group by rollup(deptno,job);51Testing软件测试网[ n4W @qO2r
--不同的deptno空两行/取消设置51Testing软件测试网2gQx&t2iK,d
break on deptno skip 2/break on null
51Testing软件测试网#_,v0dX&Dt M

51Testing软件测试网w+r!y#qlx mL

N?(Ea`*pl0

TAG:

 

评分:0

我来说两句

Open Toolbar