Getting incorrect date when converting epoch to timestamp - sql

I have a timestamp in epoch format like this: 1551187548876. If I convert this to a timestamp using https://www.epochconverter.com/, I get the correct time back.
However, if I convert it to time using PostgreSQL to_timestamp, I get the following:
select to_timestamp(1551187548876) from my_table;
51125-03-06 14:14:36+00
Question
How can I convert epoch 1551187548876 to a timestamp in PostgreSQL?

I assume that the epoch number you have is the number of milliseconds since the epoch, not the number of seconds, as customary.
Try dividing by 1000:
select to_timestamp(1551187548.876);
to_timestamp
----------------------------
2019-02-26 14:25:48.876+01
(1 row)

Related

Converting timestamp to epoch in snowflake

I have timestamp in following format: "2022-03-08 17:11:59.901" , I want to convert them to UNIX/Epoch format up to milliseconds, somehow it only delivers till seconds part.
Is there anyway to do it?
If you have a string timestamp and you want to convert to EPOCH:
SELECT to_timestamp('2022-03-08 17:11:59.901') as ts,
date_part(EPOCH_MICROSECOND ,ts)

How can I convert an integer representing EPOCH time to a timestamp in Athena (Presto)?

I have a table where the datetime is stored as varchar but represents the EPOCH time (e.g. 1556895150). How can I get that value to be recognized as a timestamp in Athena / using Presto? something like a dateadd function would work but Athena doesn't have dateadd (I envisioned something like dateadd('second',expressoin,'1970-01-01 00:00:00'. A simple CAST(expressoin as type) does not work here because EPOCH isn't a recognized datetime data type.
You can use from_unixtime():
presto> select from_unixtime(1556895150);
_col0
-------------------------
2019-05-03 07:52:30.000
(1 row)

bigQuery not supporting milliseconds timestamps

I have a value in my csv file for timetamp as '1522865628160'. When I load the data in bigQuery where this field type is timestamp, it saves the timestamp as '1522865628160000'. so when I query like
select * from <tablename> limit 1
it gives me error
Cannot return an invalid timestamp value of 1522865628160000000 microseconds relative to the Unix epoch. The range of valid timestamp values is [0001-01-1 00:00:00, 9999-12-31 23:59:59.999999]; error in writing field timestamp"
please help
I think the issue here is that you tried to load your UNIX timestamp data into a timestamp column in BigQuery. A BigQuery timestamp column is not the same thing as a UNIX timestamp. The latter is just a numerical value representing the number of seconds since the start of the UNIX epoch in 1970.
So the fix here would be to load your data into an INT64 (or INTEGER if you are using legacy) column. From there, you may convert your UNIX timestamp to a bona fide date or timestamp.
There is a MSEC_TO_TIMESTAMP() function which can convert an integer number of milliseconds since the UNIX epoch to a bona fide timestamp, e.g.
SELECT MSEC_TO_TIMESTAMP(1522865628160)
2018-04-04 11:13:48 UTC

best way to determine if two epoch timestamps are on the same date in oracle database

I have a table that contains an epoch timestamp column(transaction_date), if I have two rows and I want to determine if they are on the same day; without converting them to date using to_date. how can I do that and what is the best way that takes less operations while determining it.
An epoch "timestamp" is a number which should be generated by converting the date to UTC (if it has a timezone) and subtracting the epoch to give a number of seconds (or milliseconds) since the epoch.
Without converting to a date, you can just divide by the number of seconds (or milli-seconds) in a day and truncate to get the number of days since the epoch and then compare those values:
SELECT TRUNC( transaction_date / ( 24 * 60 * 60 ) ) FROM your_table
If the values given by two rows have an identical number of days since the epoch then they are on the same day.
Using this transformation from timestamp to day, you can compare
SELECT
* (all but transaction_date),
(EXTRACT (DAY FROM (transaction_date))*24*60*60) transaction_day
FROM yourtable;

Coverting epoch time value into a timestamp(6) in teradata

How do I covert epoch time value into a timestamp(6) in teradata ?
Lets take 1336987231051 as the example epoch time ( note this is in milliseconds, where as epoch time is in seconds), I did something like this
// miliseconds epoch time
select cast(1336987231051 as timestamp(6))
// seconds epoch time
select cast((1336987231051/1000) as timestamp(6))
and I get this error message for both of the above select statements :
[Error] Script lines: 12-12 ------------------------
[Teradata Database] [TeraJDBC 13.10.00.31] [Error 5407] [SQLState HY000] Invalid operation for DateTime or Interval.
I verified in http://www.epochconverter.com/ that 1336987231051 is valid epoch time.
What is the right sql for this in teradata ?
select
cast(cast(700101 as date) + seconds_from_epoch / 86400 as timestamp(6)) +
(seconds_from_epoch mod 86400) * interval '00:00:01' hour to second
from my_table
Simplest way to convert EPOCH to TERADATA TIME.
SELECT
1336987231051/1000 as unix_epoc_time ,
to_timestamp(unix_epoc_time) utc,
cast(cast(utc as char(19))||'+00:00' as timestamp(0) with time zone) AT LOCAL
;
If your epoch is 10 digit, you don't need /1000.
Remember, Unix time is in UTC.
Your system will treat this "utc" as local. So, let us make it understand that it is UTC by adding "+00:00" and then convert it to your LOCAL using AT LOCAL OR using either of these "America Central" , "America Eastern" , "America Mountain" etc.