Oracle PLSQL Datestamp 相減得到毫秒差
阿新 • • 發佈:2019-01-06
Oracle DateStamp 型別時間相減,得到毫秒結果小例子:
-- Created on 11/6/2013 by ZHENG.LIU DECLARE -- Local variables here i INTEGER := 1; t_start TIMESTAMP; t_end TIMESTAMP; d_start DATE; d_end DATE; BEGIN -- Test statements here SELECT systimestamp INTO t_start FROM dual; SELECT SYSDATE INTO d_start FROM dual; -- WHILE (i < 100000) LOOP --UPDATE dds_fms_config SET VALUE = 'N' WHERE NAME = 'XML_VALIDATION'; -- i := i + 1; --END LOOP; SELECT systimestamp INTO t_end FROM dual; SELECT SYSDATE INTO d_end FROM dual; --Print: dbms_output.put_line('[Timestamp]t_start = ' || t_start); dbms_output.put_line('[Timestamp]t_end = ' || t_end); dbms_output.put_line('[Timestamp - String]t_start = ' || to_char(t_start, 'yyyy-mm-dd hh24:mi:ss.ff')); dbms_output.put_line('[Timestamp - String]t_end = ' || to_char(t_end, 'yyyy-mm-dd hh24:mi:ss.ff')); dbms_output.put_line('[Timestamp]Duration = ' || (((to_date(substr(to_char(t_end, 'yyyy-mm-dd hh24:mi:ss.ff'), 1, 19), 'yyyy-mm-dd hh24:mi:ss') - to_date(substr(to_char(t_start, 'yyyy-mm-dd hh24:mi:ss.ff'), 1, 19), 'yyyy-mm-dd hh24:mi:ss')) * 24 * 3600 * 1000 * 1000 + to_number(substr(to_char(t_end, 'yyyy-mm-dd hh24:mi:ss.ff'), 21, 6)) - to_number(substr(to_char(t_start, 'yyyy-mm-dd hh24:mi:ss.ff'), 21, 6)))) / 1000 || ' ms'); dbms_output.put_line('[Date]d_start = ' || d_start); dbms_output.put_line('[Date]d_end = ' || d_end); dbms_output.put_line('[Date - String]d_start = ' || to_char(d_start, 'yyyy-mm-dd hh24:mi:ss')); dbms_output.put_line('[Date - String]d_end = ' || to_char(d_end, 'yyyy-mm-dd hh24:mi:ss')); dbms_output.put_line('[Date]Duration = ' || (d_end - d_start) * 24 * 60 * 60 || ' s'); END;
可以將此方法封裝為function, 方便計算呼叫。。。