mysql
1、日期函数
Date类型日期数据加减
-- 加法 +15天
DATE_ADD(t.APPLY_DATE,INTERVAL 15 DAY)
-- 月改为MONTH
-- 减法
DATE_SUB(NOW(),INTERVAL 1 MONTH)
timestamp类型日期数据加减
-- 计算月份
SELECT TIMESTAMPDIFF(MONTH, '2022-01-13 13:13:13','2022-10-01 16:16:16');
-- 计算天数等类似
获取当前时间
SELECT NOW(), SYSDATE();
-- now()更为常用
-- 获取当前date
SELECT CURRENT_DATE,CURRENT_DATE(),CURDATE();
将时间转为相应格式
date_format(ahc.time_, '%Y-%m-%d') as tdate
或者
date_format(t1.start_time_, '%Y-%m-%d %H:%i:%S')
在java的 .mapper文件进行时间比较时,需注意一下
date_format(t1.start_time_, '%Y-%m-%d %H:%i:%S') <= #{endTime}
< 表示小于,>表示大于
2、group_concat函数
(select GROUP_CONCAT(a.supply_name ORDER BY a.create_date DESC SEPARATOR ',') from purchase_platform_quoted_detail a where ppbbm.id = a.main_id and a.del_flag='0'
and (a.status != 'offerNew' AND a.status != 'offerCancel')) as supplyList,
将查询所得的多条数据按照时间排序后,取supply_name字段用符号 ,连接合并为一个字符串
oracle
1、时间格式处理
oracle中无法像mysql直接用>, <等判断时间数据的大小
to_char(now(), ‘yyyy-MM-dd hh24:mi:ss’)
2、 instr(字符串,目标字符串)函数
结果为第一次出现 目标字符串的位置(从1开始)
select instr(nofirm.DIGITAL_SALES_ID, ',') from nofirm
3、split函数
oracle没有分割取数函数,需要自己新建一个function
create or replace function SPLITER(p_value varchar2 ,
p_split varchar2 := ',' ,times integer := 1)
--参数1 表示字符串,参数2 为分隔符,参数3 为第几个
return varchar2 as
v_idx1 integer;
v_idx2 integer;
v_strs_last varchar2(4000 ) := p_value;
begin
if times= 1 then
return substr(v_strs_last,1 ,instr(v_strs_last,p_split,1, 1)-1 );
end if;
v_idx1 := instr(v_strs_last, p_split, 1,times-1 );
v_idx2 := instr(v_strs_last, p_split, 1,times);
if v_idx2= 0 then
return substr(v_strs_last,v_idx1+1 );
end if;
return substr(v_strs_last,v_idx1+ 1,v_idx2-v_idx1-1 );
end SPLITER;
用法如下:
select SPLITER('afageaf,sfeege,1344332', ',' , 1);