数据库的常用函数整理(达梦数据库)
----------------------------字符串函数---------------------------
--1、两者取其大 great/greatest
select great(2,6) from dual--6
--2、两者取其小
select least(2,6) from dual--2
--3、获取日期(2022-07-15)所在月份的最后一天的日期
select last_day('2022-07-15') from dual--2022-07-31
--4、向上取整
select ceil(4.2) from dual--5
--5、向下取整
select floor(4.7) from dual--4
--6、获取两个日期之间的所有日期('2022-07-01'~'2022-07-03')
select to_char(to_date('2022-07-01','yyyy-MM-dd')+rownum-1,'yyyy-MM-dd')day_id
from dual
connect by rownum<=to_date('2022-07-03','yyyy-MM-dd')-to_date('2022-07-01','yyyy-MM-dd')+1
--7、字符串截取 从第1位开始取7位
select substr('2022-07-15',1,7) from dual--2022-07
--8、查找字符串2在字符串1中出现的位置
select instr('helloworld','l') from dual; --返回结果:3 默认第一次出现“l”的位置
select instr('helloworld','lo') from dual; --返回结果:4 即:在'lo'中,'l'开始出现的位置
select instr('helloworld','wo') from dual; --返回结果:6 即'w'开始出现的位置
--可以应用于模糊查询
select * from da.t_bas_unit where instr(unitname,'日照')>0--等价于
select * from da.t_bas_unit where unitname like '%日照%'
--9、直接截取,不进行四舍五入
select trunc(2.869,2) from dual--2.86
--10、字符串连接函数
/**字符串连接函数
(1)concat()函数:可以将多个字符串连接起来
(2)|| 连接符:可以将多个字符串连接起来
*/
select concat('a','b','c','6') from dual--abc6
select 'a'||'b'||'c' from dual--abc
--11、将字符串的首字母设置为大写
select initcap('hellowo'),initcap('hEllo') from dual--Hellowo,Hello
--12、获取长度
select length('hello'),length(50) from dual--5,2
--13、填充函数
/**
填充函数:
(1)左侧填充:lpad('he',5,'l')表示如果字符串'he'长度不满5,用'l'在左侧填充;
(2)右侧填充:rpad('he',5,'l')表示如果字符串'he'长度不满5,用'l'在右侧填充;
*/
select lpad('he',5,'l'),lpad('hh',6,'abc') from dual--lllhe,abcahh
select rpad('he',5,'l') from dual--helll
--14、替换函数 将'hellohol'中的'll'替换成'a'
select replace('hellohol','ll','啦') from dual --he啦ohol
--15、去除首尾空格
select trim(' hello ')from dual--hello
--16、符号函数 大于0返回1、等于0返回0、小于0返回-1
select sign(12) from dual--1
--17、幂运算 4的2次方=16
select power(4,2) from dual--16.0
--开平方根运算 根号16=4
select sqrt(16) from dual--4.0
--18、取模(取余)
select mod(3,2)--1
--19、取绝对值
select abs(-19.6) from dual--19.6
----------------------------日期函数---------------------------
--1、获取到所在月的第一天
select trunc('2022-07-15','mm') from dual --2022-07-01 00:00:00.000000
--2、获取到所在年的第一天
select trunc('2022-07-15','yyyy') from dual --2022-01-01 00:00:00.000000
--3、取周的开始时间和结束时间
SELECT to_char(trunc('2022-08-24','iw'),'yyyy-mm-dd') AS STARTDATE FROM DUAL;--本周周一
SELECT to_char(trunc('2022-08-24','iw')+6,'yyyy-mm-dd') AS ENDDATE FROM DUAL;--本周周日
SELECT to_char(trunc('2022-08-24','iw')-7,'yyyy-mm-dd') AS STARTDATE FROM DUAL;---上周周一
SELECT to_char(trunc('2022-08-24','iw')-1,'yyyy-mm-dd') AS ENDDATE FROM DUAL;--上周周日
--4、获取该日期所在月的最后一天
select last_day('2022-07-15') from dual--2022-07-31
--5、转换为规定格式
select to_char('2022-07-15','yyyy-mm') from dual--2022-07
--6、months_between函数
select trunc(months_between(sysdate, to_date('2022-01-01','yyyy-mm-dd')) / 12) from dual;
--相差年份(两个日期之间相差了多少年)
select round((months_between(TO_DATE('2018-03-31'),TO_DATE('2016-05-31')))/12,2)from dual;--1.83
--两个日期相差的天数
select to_date('2022-07-15','yyyy-mm-dd')-to_date('2022-07-12','yyyy-mm-dd') from dual
--7、add_months函数
--加1月
select sysdate,add_months(sysdate,1) from dual;--2022-07-15 17:10:26 2022-08-15
--减1月
select sysdate,add_months(sysdate,-1) from dual;--2022-07-15 17:10:42 2022-06-15
--加1天
select sysdate,to_char(sysdate+1,'yyyy-mm-dd HH24:MI:SS')from dual;--2022-07-15 17:10:56 2022-07-16 17:10:56
--减1天
select sysdate,to_char(sysdate-1,'yyyy-mm-dd HH24:MI:SS')from dual;--2022-07-15 17:11:08 2022-07-14 17:11:08
select to_date(null)