--数字函数:针对的对象的数据类型是数字ABS(X):返回X的绝对值mod(x,y):返回X除以Y的余数power(x,y):返回X的Y次幂的结果--比如select abs(-2), mod(7,3), power(2,3)from dual;
ceil(x):向上取整 --X轴坐标从左到右(负无穷到正无穷),取这个数字的右侧的最近的那个整数floor(x):向下取整 --X轴坐标从左到右(负无穷到正无穷),取这个数字的左侧的最近的那个整数
--比如select ceil(-3.99),ceil(3.4), floor(-3.1),floor(3.4)from dual;
round(x,y):返回X四舍五入后的结果 Y是 正整数 时:表示在小数点右边第Y位四舍五入 不写Y 时 :表示在整数位四舍五入,舍去小数,等同于Y=0 Y是 负整数 时:表示在小数点左边第Y位四舍五入--比如:select round(3.1415,2),round(3.1415,3), round(3.1415),round(3.1415,0), round(3.1415,-1),round(5.1415,-1), round(123.12,-2),round(153.12,-2)from dual;
trunc(x,y):返回X截取(干掉)后的结果 Y是 正整数 时:表示在小数点右边第Y位截取 不写Y 时 :表示在整数位截取,只保留整数,等同于Y=0 Y是 负整数 时:表示在小数点左边第Y位截取
--比如:select trunc(3.1415,2),trunc(3.1419,3), trunc(3.1415),trunc(3.1415,0), trunc(3.1415,-1),trunc(5.1415,-1), trunc(123.12,-2),trunc(153.12,-2)from dual;
---字符函数:针对的对象的数据类型是字符
length(x):返回X的字符长度replace(x,y,z):从字符串X中找到Y,并替换成z,返回替换后的结果--比如:select replace('hello world','o','xxx')from dual;
substr(x,y[,z]):从字符串X的第Y开始,截取z个长度的字符,返回截取出的字符。不写z的话,表示截取到字符结尾--比如:select substr('hello world',4,5), --一个空格也占一个长度 SUBSTR('hello world',4), SUBSTR('hello world',-2) --Y是负整数时,从字符串最右侧往左数位置from dual;
ltrim(x[,y]):从字符串X的最左侧开始,截取掉(干掉)里面的Y这个字符,直到遇到第一个不是Y的字符为止。返回截取后剩余的字符。不写Y时,表示干掉空格。--比如:select ltrim('aaasasdf','a'), ltrim(' sda a'), ltrim('a aa shf',' a') ---如果字符Y的长度不只1位,那么分别把Y中的每一个字符都拿来在X中最左侧开始匹配,遇到了就全干掉,直到遇到不是Y中的任何一个字符为止from dual;
rtrim(x[,y]):从字符串X的最右侧开始,截取掉(干掉)里面的Y这个字符,直到遇到第一个不是Y的字符为止。返回截取后剩余的字符。不写Y时,表示干掉空格。--比如:select rtrim('aaasasdf','a'), rtrim(' sda a '), rtrim('shfa aa ',' a') --如果字符Y的长度不只1位,那么分别把Y中的每一个字符都拿来在X中最右侧开始匹配,遇到了就全干掉,直到遇到不是Y中的任何一个字符为止from dual;
trim([y from ]x):从字符串X的两侧开始,截取掉(干掉)里面的Y这个字符,直到遇到第一个不是Y的字符为止。返回截取后剩余的字符。不写 Y from 时,表示干掉空格。 --比如:select trim('a' from 'aaasasdfaaaaaaa'), trim(' sda a ') --,trim(' a' from 'shfa aa ') -- trim里面的Y(要干掉的字符)只能是一个字符长度的值from dual; instr(x,y,m[,n]):从字符串X中,可以从指定位置(m)开始,直到里面的字符Y第N次出现的位置 --返回的是一个数字 单独不写n:表示从指定位置(m)开始,找第1次出现的位置 同时不写m和n:表示从第1位开始,找第1次出现的位置 如果没有找到,返回0--比如:select instr('t%yug#$%yuihi#¥%vhkhl%^','%',3,2), instr('t%yug#$%yuihi#¥%vhkhl%^','%',3,10)from dual;
select instr(ename,'A'),enamefrom emp;
|| :字符串连接符--比如:select 'asd' ||' '||'@#$' from dual;
select ename || '的工资是: ' || sal, ename || '的入职日期是:' || hiredate, ename || '的入职日期是:' || to_char(hiredate, 'yyyy-mm-dd'),
ename || '的入职日期是:' || to_char(hiredate, 'yyyy') || '年' ||
to_char(hiredate, 'mm') || '月' || to_char(hiredate, 'dd') || '日' from emp;
concat(x,y):返回字符串X和字符串Y连接起来后的新的字符串--比如:select concat(ename,sal), concat(concat(ename,'的工资是:'),sal)from emp;
---注意:一个特别的聚合函数(分组函数),不是用于统计:wm_concatwm_concat(目标字段):将目标字段的值用 , 分隔显示成一行
--比如:select wm_concat(ename)from emp;
--
select deptno,wm_concat(ename)from empgroup by deptno;
---日期函数:针对的数据是日期型add_months(d,n):返回日期D加上N个月后的日期--比如:select add_months(sysdate,2) from dual;
months_between(d1,d2):返回日期d1跟日期d2之间相隔的月份 -- D1 - D2--比如:select Months_between(to_date(20201212,'yyyymmdd'),to_date(20200812,'yyyymmdd'))from dual;
last_day(d):返回日期D所处月份的最后一天--比如:select last_day(sysdate) from dual;
round(d[,fmt]):对日期的按照不同的维度(fmt)进行四舍五入,返回四舍五入后的日期1、fmt是 year/yyyy:表示对 年 进行四舍五入,返回d所处年份的1月1号或者次年的1月1号 --以 0701 为界限:>=0701:进,<0701:舍 --比如: select round(to_date(20200630,'yyyymmdd'),'year'), round(to_date(20200701,'yyyymmdd'),'yyyy') from dual; 2、fmt是 month/mm:表示对 月 进行四舍五入,返回d所处月份的1号或者次月的1号 --以 16号 为界限:>=16号:进,<16号:舍 --比如: select round(to_date(20200215,'yyyymmdd'),'month'), round(to_date(20200216,'yyyymmdd'),'mm') from dual;
3、fmt是 ddd/dd 或者 不写fmt时:表示对 日 进行四舍五入。返回d所处日期或者次日 --以 12:00:00 为界限:>=12:00:00:进, <12:00:00:舍 --比如: select round(to_date('20200216 11:59:59','yyyymmdd hh24:mi:ss'),'ddd'), round(to_date('20200216 12:00:00','yyyymmdd hh24:mi:ss'),'dd'), round(to_date('20200216 12:00:01','yyyymmdd hh24:mi:ss')) from dual;
4、fmt是 day 时:表示对 周 进行四舍五入。返回上周的周日的日期或者本周的周日的日期 -- 以 周四 为界限:>=周四:进 , <周四:舍 select round(to_date('20210324','yyyymmdd'),'day'), --周三 round(to_date('20210325','yyyymmdd'),'day') --周四 from dual;
5、fmt是 q 时:表示对 季度 进行四舍五入,返回当前d所处季度的第一天的日期或者下一个季度第一天的日期 --以 季度中间那个月份的16号为界限:>=16号:进 <16号:舍 select round(to_date('20210515','yyyymmdd'),'q'), round(to_date('20210516','yyyymmdd'),'q') from dual;
---日期的几大维度:年、月、日、周、季度select to_char(hiredate,'yyyy'), to_char(hiredate,'mm'), to_char(hiredate,'dd'), to_char(hiredate,'day'), to_char(hiredate,'q')from emp;
trunc(d[,fmt]):对日期的按照不同的维度(fmt)进行截取,返回截取后的日期
1、fmt是 year/yyyy:表示对 年 进行截取,返回d所处年份的1月1号 --比如: select trunc(to_date(20201231,'yyyymmdd'),'year'), trunc(to_date(20200701,'yyyymmdd'),'yyyy') from dual; 2、fmt是 month/mm:表示对 月 进行截取,返回d所处月份的1号 --比如: select trunc(to_date(20200215,'yyyymmdd'),'month'), trunc(to_date(20200228,'yyyymmdd'),'mm') from dual;
3、fmt是 ddd/dd 或者 不写fmt时:表示对 日 进行截取。返回d所处日期 --比如: select trunc(to_date('20200216 11:59:59','yyyymmdd hh24:mi:ss'),'ddd'), trunc(to_date('20200216 12:00:00','yyyymmdd hh24:mi:ss'),'dd'), trunc(to_date('20200216 23:59:59','yyyymmdd hh24:mi:ss')) from dual;
4、fmt是 day 时:表示对 周 进行截取。返回上周的周日的日期 select trunc(to_date('20210324','yyyymmdd'),'day'), --周三 trunc(to_date('20210325','yyyymmdd'),'day') --周四 from dual;
5、fmt是 q 时:表示对 季度 进行截取,返回当前d所处季度的第一天的日期 select trunc(to_date('20210515','yyyymmdd'),'q'), trunc(to_date('20210630','yyyymmdd'),'q') from dual;
--小练习一把:用sysdate,计算sysdate所在年份有多少天--可以用次年的1月1号 - 当年的1月1号
次年的1月1号 = round(add_months(sysdate,6),'yyyy') = trunc(add_months(sysdate,12),'yyyy')
当年的1月1号 = round(add_months(sysdate,-6),'yyyy') = trunc(sysdate,'yyyy')
select trunc(add_months(sysdate,12),'yyyy') - trunc(sysdate,'yyyy'), round(add_months(sysdate,6),'yyyy') - round(add_months(sysdate,-6),'yyyy')from dual;
END
数字/字符/日期函数
原创mob604756fc093d ©著作权
©著作权归作者所有:来自51CTO博客作者mob604756fc093d的原创作品,请联系作者获取转载授权,否则将追究法律责任
上一篇:Oracle:建表及DML语句。
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
python 日期字符串转换为指定格式的日期
python 日期字符串转换为指定格式的日期
字符串 python 返回结果 -
【C语言加油站】详细解读字符函数与字符串函数
【C语言加油站】字符函数与字符串函数详细介绍字符函数与字符串函数的相关知识点……
C语言 字符串 字符函数 字符串函数 -
Oracle trunc() 日期、数字截取函数git 取整 oracle 四舍五入 html