Oracle函数脚本记录

时间:2022-11-22 03:37:45
--内置函数
--聚合函数 返回单个值
select count(*) from student where sclass=''; --count()记录条数
select sum(degree) from score t; --sum(degree)求成绩总和
select avg(degree) from score t; --avg(degree)求成绩平均值
select max(degree),min(degree) from score t; --max(degree),min(degree)分别为成绩最大值,最小值
select max(degree),min(degree),avg(degree),sum(degree) from score t; --可以用一行将所有聚合函数全部计算出来 --转换函数
select sname,sno,sbirthday from student where to_char(sbirthday,'yyyy')= (select to_char(s.sbirthday,'yyyy') from student s where s.sno='');
--to_char 将日期转化成字符串,日期格式 'yyyy-mm-dd hh24:mi:ss'
select * from student where sbirthday >=to_date('1976-1-1','yyyy-mm-dd') --to_date 将字符串转化成日期 --时间函数
select cast('' as number) from dual;
select sysdate from dual; --获取数据库当前时间
select sysdate+1 from dual; --日期里的日进行加1日;
select add_months(sysdate,+1) from dual; --月份加1月 --数值函数
--绝对值
select abs(-123) from dual;
--求模
select mod(12,5) from dual;
--取整
--取上限值
select ceil(123.35) from dual;
--取下限值
select floor(123.35) from dual;
--四舍五入
select round(123.35) from dual;
select round(123.4567,2) from dual; --根据保留位数在四舍五入,保留两位小数
select round(123.4567,-1) from dual; --个位数进行四舍五入,以小数点作为远点进行加减
--舍去小数点
select trunc(123.35) from dual;
select trunc(123.35,1) from dual; --字符串函数
--计算长度
select sname, langth(sname) from student;
--去空格
select trim(' asas ') from dual;
select ltrim(' asas ') from dual; --左侧去空格
select rtrim(' asas ') from dual; --右侧去空格
--替换
select replace(' ab cd ',' ','') from dual;
select replace(sname,'王','liu') from student where sname like '王%';
--查找字符串
select instr('abcd','a') from dual; --查询索引值从1开始,找不到返回0
--截取字符串
select substr('abcde','') from dual; --0或者1都是从1位开始往后截取,到最后
select substr('abcde',-1) from dual; --从右侧进行截取,截取结果为e
select substr('abcde',2,3) from dual; --截取结果为bcd,从截取位置开始算取3位长度
select substr('abcde',-2,3) from dual; --截取结果为dc,从最右边开始截取到3个长度
select aname,substr(sname,1,1)||'同学' from student; --其他函数
select avg(nvl(degree,0)) from score; --将空值赋个默认值为0,在进行平均数计算;
select t.* decode(ssex,'','男','','女','不知道') from STUDENT t; --将1换成男,2换成女,如果没有,显示‘不知道’