文章目录
- 环境
- 症状
- 问题原因
- 解决方案
环境
系统平台:N/A
版本:4.3.4.6
症状
MYSQL中正常执行的业务SQL报错,找不到timestampdiff函数。
问题原因
在做MYSQL到Highgo DB 迁移适配工作时,客户大量使用了timestampdiff 函数,修改比较麻烦,希望可以使用同名函数来替代。
解决方案
CREATEORREPLACEFUNCTIONTIMESTAMPDIFF(p_what varchar2,p_d1timestamp,p_d2timestamp)RETURNSnumericLANGUAGEplpgsqlAS$function$DECLAREl_result number;t_result number;beginl_result:=null;--秒if(LOWER(p_what)='second')thenselectfloor(extract(epochfrom(p_d2-p_d1)))intot_result;l_result :=t_result;endif;--小时if(LOWER(p_what)='minute')thenselectfloor(extract(epochfrom(p_d2-p_d1))/60)intot_result;l_result :=t_result;endif;--天if(LOWER(p_what)='hour')thenselectfloor(extract(epochfrom(p_d2-p_d1))/60/60)intot_result;l_result :=t_result;endif;if(LOWER(p_what)='day')thenselectfloor(extract(dayfrom(p_d2-p_d1)))intot_result;l_result :=t_result;ENDIF;returnl_result;end;$function$示例:
selectTIMESTAMPDIFF('SECOND','2018-03-20 09:00:00','2018-03-22 10:00:01');