plsql函数

上传人:n**** 文档编号:88890297 上传时间:2019-05-12 格式:DOC 页数:13 大小:72KB
返回 下载 相关 举报
plsql函数_第1页
第1页 / 共13页
plsql函数_第2页
第2页 / 共13页
plsql函数_第3页
第3页 / 共13页
plsql函数_第4页
第4页 / 共13页
plsql函数_第5页
第5页 / 共13页
点击查看更多>>
资源描述

《plsql函数》由会员分享,可在线阅读,更多相关《plsql函数(13页珍藏版)》请在金锄头文库上搜索。

1、plsql常用方法在SQLPLUS下,实现中-英字符集转换alter session set nls_language=AMERICAN;alter session set nls_language=SIMPLIFIED CHINESE;主要知识点:一、有关表的操作1)建表create table test as select * from dept; -从已知表复制数据和结构create table test as select * from dept where 1=2; -从已知表复制结构但不包括数据2)插入数据:insert into test select * from dept;二

2、、运算符算术运算符:+ - * / 可以在select 语句中使用连接运算符:| select deptno| dname from dept;比较运算符: = = != = like between is null in逻辑运算符:not and or集合运算符: intersect ,union, union all, minus要求:对应集合的列数和数据类型相同查询中不能包含long 列列的标签是第一个集合的标签使用order by时,必须使用位置序号,不能使用列名例:集合运算符的使用:intersect ,union, union all, minusselect * from emp

3、 intersect select * from emp where deptno=10 ;select * from emp minus select * from emp where deptno=10;select * from emp where deptno=10 union select * from emp where deptno in (10,20); -不包括重复行select * from emp where deptno=10 union all select * from emp where deptno in (10,20); -包括重复行三,常用 ORACLE 函

4、数sysdate为系统日期 dual为虚表一)日期函数重点掌握前四个日期函数1,add_months返回日期加(减)指定月份后(前)的日期select sysdate S1,add_months(sysdate,10) S2,add_months(sysdate,5) S3 from dual;2,last_day 返回该月最后一天的日期select last_day(sysdate) from dual;3,months_between返回日期之间的月份数select sysdate S1, months_between(1-4月-04,sysdate) S2,months_between(

5、1-4月-04,1-2月-04) S3 from dual4,next_day(d,day): 返回下个星期的日期,day为1-7或星期日-星期六,1表示星期日select sysdate S1,next_day(sysdate,1) S2,next_day(sysdate,星期日) S3 FROM DUAL5,round舍入到最接近的日期(day:舍入到最接近的星期日)select sysdate S1,round(sysdate) S2 ,round(sysdate,year) YEAR,round(sysdate,month) MONTH ,round(sysdate,day) DAY

6、from dual6,trunc截断到最接近的日期select sysdate S1,trunc(sysdate) S2,trunc(sysdate,year) YEAR,trunc(sysdate,month) MONTH ,trunc(sysdate,day) DAY from dual7,返回日期列表中最晚日期select greatest(01-1月-04,04-1月-04,10-2月-04) from dual二)字符函数(可用于字面字符或数据库列)1,字符串截取select substr(abcdef,1,3) from dual2,查找子串位置select instr(abcfd

7、gfdhd,fd) from dual3,字符串连接select HELLO|hello world from dual;4, 1)去掉字符串中的空格select ltrim( abc) s1,rtrim(zhang ) s2,trim( zhang ) s3 from dual2)去掉前导和后缀select trim(leading 9 from 9998767999) s1,trim(trailing 9 from 9998767999) s2,trim(9 from 9998767999) s3 from dual;5,返回字符串首字母的Ascii值select ascii(a) fro

8、m dual6,返回ascii值对应的字母select chr(97) from dual7,计算字符串长度select length(abcdef) from dual8,initcap(首字母变大写) ,lower(变小写),upper(变大写)select lower(ABC) s1,upper(def) s2,initcap(efg) s3 from dual;9,Replaceselect replace(abc,b,xy) from dual;10,translateselect translate(abc,b,xx) from dual; - x是1位11,lpad 左添充 rp

9、ad 右填充(用于控制输出格式)select lpad(func,15,=) s1, rpad(func,15,-) s2 from dual;select lpad(dname,14,=) from dept;12, decode实现if .then 逻辑select deptno,decode(deptno,10,1,20,2,30,3,其他) from dept;三)数字函数1,取整函数(ceil 向上取整,floor 向下取整)select ceil(66.6) N1,floor(66.6) N2 from dual;2, 取幂(power) 和 求平方根(sqrt)select po

10、wer(3,2) N1,sqrt(9) N2 from dual;3,求余select mod(9,5) from dual;4,返回固定小数位数 (round:四舍五入,trunc:直接截断)select round(66.667,2) N1,trunc(66.667,2) N2 from dual;5,返回值的符号(正数返回为1,负数为-1)select sign(-32),sign(293) from dual;四)转换函数1,to_char()将日期和数字类型转换成字符类型1) select to_char(sysdate) s1,to_char(sysdate,yyyy-mm-dd)

11、 s2,to_char(sysdate,yyyy) s3,to_char(sysdate,yyyy-mm-dd hh12:mi:ss) s4,to_char(sysdate, hh24:mi:ss) s5,to_char(sysdate,DAY) s6 from dual;2) select sal,to_char(sal,99999) n1,to_char(sal,99,999) n2 from emp2, to_date()将字符类型转换为日期类型insert into emp(empno,hiredate) values(8000,to_date(2004-10-10,yyyy-mm-d

12、d);3, to_number() 转换为数字类型select to_number(to_char(sysdate,hh12) from dual; /以数字显示的小时数五)其他函数user:返回登录的用户名称select user from dual;vsize:返回表达式所需的字节数select vsize(HELLO) from dual;nvl(ex1,ex2):ex1值为空则返回ex2,否则返回该值本身ex1(常用)例:如果雇员没有佣金,将显示0,否则显示佣金select comm,nvl(comm,0) from emp;nullif(ex1,ex2):值相等返空,否则返回第一个值

13、例:如果工资和佣金相等,则显示空,否则显示工资select nullif(sal,comm),sal,comm from emp;coalesce:返回列表中第一个非空表达式select comm,sal,coalesce(comm,sal,sal*10) from emp;nvl2(ex1,ex2,ex3) :如果ex1不为空,显示ex2,否则显示ex3如:查看有佣金的雇员姓名以及他们的佣金select nvl2(comm,ename,) as HaveCommName,comm from emp;六)分组函数max min avg count sum1,整个结果集是一个组1) 求部门30

14、的最高工资,最低工资,平均工资,总人数,有工作的人数,工种数量及工资总和select max(ename),max(sal),min(ename),min(sal),avg(sal),count(*) ,count(job),count(distinct(job) ,sum(sal) from emp where deptno=30;2, 带group by 和 having 的分组1)按部门分组求最高工资,最低工资,总人数,有工作的人数,工种数量及工资总和select deptno, max(ename),max(sal),min(ename),min(sal),avg(sal),count

15、(*) ,count(job),count(distinct(job) ,sum(sal) from emp group by deptno;2)部门30的最高工资,最低工资,总人数,有工作的人数,工种数量及工资总和select deptno, max(ename),max(sal),min(ename),min(sal),avg(sal),count(*) ,count(job),count(distinct(job) ,sum(sal) from emp group by deptno having deptno=30;3, stddev 返回一组值的标准偏差select deptno,stddev(sal) from emp group by deptno

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 高等教育 > 其它相关文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号