内置函数(2)

 1 --数值函数
 2 select abs(-123) from dual--求绝对值
 3 select mod(13,3) from dual--取模
 4 select ceil(123.3) from dual--向上取整
 5 select floor(123.3) from dual--向下取整
 6 
 7 select round(123.65) from dual--四舍五入,保留整数
 8 select round(123.65,1) from dual--四舍五入,保留小数点后一位
 9 select round(126.65,-1) from dual--四舍五入,保留整数,使其各位数变为0
10 
11 select trunc(123.65) from dual--截取整数部分
12 select trunc(123.65,1) from dual--截取小数点后一位
13 select trunc(129.65,-1) from dual--截取整数部分,使其各位数变为0
14 
15 
16 
17 
18 --字符串函数
19 select t.sname,length(sname) from STUDENT t--获取字符串长度
20 select t.sname,substr(sname,2,1) from STUDENT t--从第2个字符开始截取,截取1个字符的长度出来
21 select replace('abcde','b','B') from dual--把所有小写的b替换成大写的B
22 select sname,trim(sname) from STUDENT t--trim()去除前后空格,则ltrim()去除左空格,rtrim()去除右空格
23 
24 
25 
26 
27 --时间函数
28 select sysdate from dual--系统当前时间
29 select sysdate +20 from dual--当前时间加上20天
30 select add_months(sysdate,1) from dual--当前时间加上1个月
31 select last_day(add_months(sysdate,-1)) from dual--上个月的最后一天
32 
33 
34 
35 
36 --转换函数
37 select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') from dual--把时间转成字符串
38 select to_date('2016-01-01 15;00:43','yyyy-mm-dd hh24:mi:ss') from dual--把字符串转成时间
39 select to_number('123.56') from dual--把字符串转成数值型
1 --通用转换函数
2 select '成绩:' || cast(t.degree as varchar2(20))from SCORE t--在某列数据前面可插入临时数据用||表示连接通用函数(指定某一列 as 转换什么类型)
3 select cast(t.degree as number+100 from SCORE t--转成数字型,统一加100
1 --空函数1
2 select t.sno,t.sname,NVL(t.class,'班级') from STUDENT t--如果某列有空数据时,把空值变为班级
3 --空函数2
4 select t.sno,t.sname,NVL(t.class,'班级''') from STUDENT t--中间缺省值为没有空数据时替换为***,后面的缺省值为把有空值变为***
5 --decode函数 
6 select t.sno,decode(t.ssex,'''1','','2','不清楚') from STUDENT t--在ssex列中,如果有男则变为1显示,如果有女则变为2显示,最后一个值为不确定的数据,则显示为不清楚

 

posted @ 2016-06-16 22:36  明天会更好!!!!  阅读(138)  评论(0)    收藏  举报