在工作中需要计算两个时间的差值,结束时间 - 开始时间,又不想在js里写function,也不想在Java里去计算,干脆就在Oracle数据库做了一个函数来计算两个时间的差值。格式为XX天XX时XX分XX秒;
上代码:
CREATE OR REPLACE FUNCTION F_GET_DIFF_TIME(START_TIME IN DATE, END_TIME IN DATE) RETURN VARCHAR2 IS DIFF_TIME VARCHAR2(50); BEGIN SELECT TDAY || '天' || THOUR || '时' || TMINUTE || '分' || ROUND((TT - TMINUTE) * 60) || '秒' INTO DIFF_TIME FROM (SELECT TDAY, THOUR, TRUNC((TT - THOUR) * 60) TMINUTE, (TT - THOUR) * 60 TT FROM (SELECT TDAY, TRUNC((TT - TDAY) * 24) THOUR, (TT - TDAY) * 24 TT FROM (SELECT TO_NUMBER(END_TIME - START_TIME) AS TT, TRUNC(TO_NUMBER(END_TIME - START_TIME)) AS TDAY FROM (SELECT START_TIME, END_TIME FROM dual))));
RETURN DIFF_TIME; END;
输出的格式为下图: