kingbase——创建timestampdiff函数
创建函数如下:
CREATE OR REPLACE FUNCTION "ops_data"."timestampdiff"("para1" text, "para2" "sys"."timestamp", "para3" "sys"."timestamp") RETURNS "pg_catalog"."int8" AS $BODY$ declare diff interval day to second; diffs bigint; diffm bigint; diffh bigint; diffd bigint; nyy bigint; ny bigint; nm bigint; begin diff:=para3-para2; diffs:=extract(second from diff); diffm:=extract(minute from diff); diffh:=extract(hour from diff); diffd:=extract(day from diff); ny:=extract(year from age(para3,para2)); nm:=extract(month from age(para3,para2)); nyy:=extract(year from para3)-extract(year from para2); if para1='second' then return ((diffd*24+diffh)*60+diffm)*60+diffs; elseif para1='minute' then return (diffd*24+diffh)*60+diffm; elseif para1='hour' then return diffd*24+diffh; elseif para1='day' then return diffd; elseif para1='month' then return ny*12+nm; elseif para1='year' then return nyy; else raise warning 'para1 not support "%"!',para1; return null; end if; end$BODY$ LANGUAGE plsql VOLATILE COST 100
timestampdiff函数: TIMESTAMPDIFF() 函数返回两个日期/日期时间之间的时间间隔。
示例如下:
TIMESTAMPDIFF(unit, datetime1, datetime2)
unit
必需的。时间间隔的单位,可用值有:** MICROSECOND, SECOND, MINUTE, HOUR, DAY, WEEK, MONTH, QUARTER, YEAR.**