決定 Oracle 時間戳記中的毫秒差
使用 Oracle 時間戳通常需要計算兩個值之間的精確時間差。雖然秒或分鐘的差異很常見,但在特定應用中計算精確到毫秒的差異可能至關重要。
方法
EXTRACT
函數提供了一個解。 它從 INTERVAL DAY TO SECOND
值中提取組件 - 減去兩個時間戳記的結果。
此初始查詢擷取日、小時、分鐘和秒組成部分:
<code class="language-sql">SELECT EXTRACT(DAY FROM diff) AS days, EXTRACT(HOUR FROM diff) AS hours, EXTRACT(MINUTE FROM diff) AS minutes, EXTRACT(SECOND FROM diff) AS seconds FROM (SELECT SYSTIMESTAMP - TO_TIMESTAMP('2012-07-23', 'YYYY-MM-DD') AS diff FROM DUAL);</code>
要取得總毫秒數,請轉換這些分量並求和:
<code class="language-sql">SELECT EXTRACT(DAY FROM diff) * 24 * 60 * 60 * 1000 + EXTRACT(HOUR FROM diff) * 60 * 60 * 1000 + EXTRACT(MINUTE FROM diff) * 60 * 1000 + ROUND(EXTRACT(SECOND FROM diff) * 1000) AS total_milliseconds FROM (SELECT SYSTIMESTAMP - TO_TIMESTAMP('2012-07-23', 'YYYY-MM-DD') AS diff FROM DUAL);</code>
重要提示: ROUND
函數確保秒以毫秒為單位準確表示。
雖然可以計算總毫秒數,但保留 INTERVAL DAY TO SECOND
結果或為每個時間單位使用單獨的列通常更實用。 然而,這種方法可以在需要時提供精確的毫秒計算。
以上是如何計算兩個Oracle時間戳記之間的毫秒差異?的詳細內容。更多資訊請關注PHP中文網其他相關文章!