簡體   English   中英

在Oracle SQL中將時間戳轉換為Unix紀元時間

[英]Converting timestamp to unix epoch time in Oracle SQL

我在Oracle中有一個20M記錄的表,其中datetime(列之一)列為VARCHAR類型,在EST時區中。 我正在嘗試創建一個將datetime轉換為Unix紀元時間的新列,但它給我拋出了一個錯誤。

create function unix_timestamp(pi_date date) return number is
    c_base_date constant date := to_date('1970-01-01 00:00:00', 'YYYY-MM-DD HH:MM:SS');
    c_seconds_in_day constant number := 24 * 60 * 60;
    v_unix_timestamp number;
begin
    v_unix_timestamp := trunc((pi_date - c_base_date) * c_seconds_in_day);

    if (v_unix_timestamp < 0 ) then
        raise_application_error(-20000, 'unix_timestamp:: unix_timestamp cannot be nagative');
    end if;

    return v_unix_timestamp;
end unix_timestamp;

這是我創建的函數,當我嘗試調用此函數時,它向我拋出一條錯誤消息:

ORA-01861: literal does not match format string
01861. 00000 -  "literal does not match format string"
*Cause:    Literals in the input must be the same length as literals in
           the format string (with the exception of leading whitespace).  If the
           "FX" modifier has been toggled on, the literal must match exactly,
           with no extra whitespace.
*Action:   Correct the format string to match the literal.

我可以得到一些幫助嗎?

代碼行

c_base_date constant date := to_date('1970-01-01 00:00:00', 'YYYY-MM-DD HH:MM:SS');

時分格式代碼錯誤; 嘗試

c_base_date constant date := to_date('1970-01-01 00:00:00', 'YYYY-MM-DD HH24:MI:SS');

MI數分鍾(我已經忘記了要數的次數超出了我的計算),並且以“小時”格式( HH24 )包括24。 還要查看MathGuy對時區的評論和對date數據類型的評論,其中不包括時區信息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM