Oracle函数脚本记录

--内置函数
--聚合函数 返回单个值
select count(*) from student where sclass='95031';   --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='108');
--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('123' 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','1') 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,'1','''2','','不知道') from STUDENT t;  --将1换成男,2换成女,如果没有,显示‘不知道’

 

转载于:https://www.cnblogs.com/tfl-511/p/5975938.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值