Converting timestamp to unix epoch time in Oracle SQL
NickName:NAB0815 Ask DateTime:2019-07-01T23:07:38

Converting timestamp to unix epoch time in Oracle SQL

I have a table of 20M records in Oracle with a datetime(one of the columns) column of type VARCHAR and in EST timezone. I am trying to create a new column converting the datetime to Unix epoch time but it is throwing me an error.

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;

This is the function I created and when I try to call this function, It is throwing me an error saying:

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.

Can I get some help with it?

Copyright Notice:Content Author:「NAB0815」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/56838075/converting-timestamp-to-unix-epoch-time-in-oracle-sql

More about “Converting timestamp to unix epoch time in Oracle SQL” related questions

Converting timestamp to unix epoch time in Oracle SQL

I have a table of 20M records in Oracle with a datetime(one of the columns) column of type VARCHAR and in EST timezone. I am trying to create a new column converting the datetime to Unix epoch time...

Show Detail

Oracle Range partition based on Unix Epoch Timestamp

Is it possible for us to range partition a table which has unique column as timestamp stored in oracle in Unix Epoch format . There are lot of options we have w.r.t to oracle partition but I want to

Show Detail

Converting Unix epoch to date/timestamp with time zone in Oracle SQL

I'm trying to write SQL expression that will take a unix epoch (seconds since 1970/1/1) to local time in specific time zone and extract the hour value from it. After researching solutions along thi...

Show Detail

converting Epoch timestamp to sql server(human readable format)

I have a problem in converting the Unix timestamp to sql server timestamp. I have a data in excel sheet and I will import that data through a tool. So I am looking for a code or syntax which can c...

Show Detail

converting Epoch timestamp to sql server(human readable format)

I have a problem in converting the Unix timestamp to sql server timestamp. I have a data in excel sheet and I will import that data through a tool. So I am looking for a code or syntax which can c...

Show Detail

Convert unix epoch timestamp to TSQL datetime

I have found only one similar question but for MySQL. I was working on a web service and had to query the database (MS SQL server). Since I couldn't get the right result I decided to test the que...

Show Detail

Convert a double (or string) into a Unix epoch timestamp in MySQL?

I've seen the instructions for converting back and forth between a UNIX epoch and a datetime in MySQL -- it's not too hard (from_unixtime to convert from epoch to datetime, unix_timestamp to get the

Show Detail

Converting PostgreSql timestamp query to MS SQL/Azure SQL

I'm currently in the process of converting some basic sql scripts from Postgresql to Azure Sql. I'm newbie to sql, but I really can't understand epoch/unix time in Azure SQL / SQL Server. My appli...

Show Detail

Converting PostgreSql timestamp query to MS SQL/Azure SQL

I'm currently in the process of converting some basic sql scripts from Postgresql to Azure Sql. I'm newbie to sql, but I really can't understand epoch/unix time in Azure SQL / SQL Server. My appli...

Show Detail

oracle convert unix epoch time to date

The context is that there is an existing application in our product which generates and sends the EPOCH number to an existing oracle procedure &amp; vice versa. It works in that procedure using som...

Show Detail