环境
系统平台:N/A
版本:4.3.4.6
症状
MYSQL中正常执行的业务SQL报错,找不到timestampdiff函数。
问题原因
在做MYSQL到Highgo DB 迁移适配工作时,客户大量使用了timestampdiff 函数,修改比较麻烦,希望可以使用同名函数来替代。
解决方案
CREATE OR REPLACE FUNCTION TIMESTAMPDIFF(p_what varchar2, p_d1 timestamp, p_d2 timestamp)
RETURNS numeric
LANGUAGE plpgsql
AS $function$
DECLARE
l_result number;
t_result number;
begin
l_result:=null;
--秒
if (LOWER(p_what) = 'second') then
select floor(extract(epoch from (p_d2-p_d1))) into t_result;
l_result := t_result;
end if;
--小时
if (LOWER(p_what) = 'minute') then
select floor(extract(epoch from (p_d2-p_d1))/60) into t_result;
l_result := t_result;
end if;
--天
if (LOWER(p_what) = 'hour') then
select floor(extract(epoch from (p_d2-p_d1))/60/60) into t_result;
l_result := t_result;
end if;
if (LOWER(p_what) = 'day') then
select floor(extract(day from (p_d2-p_d1))) into t_result;
l_result := t_result;
END IF;
return l_result;
end;
$function$
示例:
select TIMESTAMPDIFF('SECOND', '2018-03-20 09:00:00', '2018-03-22 10:00:01');
1435

被折叠的 条评论
为什么被折叠?



