How to convert epoch to datetime in bigquery? - sql

I have a column in a bigquery table with epoch values in milliseconds. These include negative epoch values for dates before 1970 also.
How do I convert them into DATETIME format using Standard and Legacy SQL to the format:1998-10-18 13:45:55?
This should work even for dates before 1970 .i.e. negative epoch values.
I tried EXTRACT(DATETIME FROM TIMESTAMP_MILLIS(-2494865480000));
But it returns me a value with a T included in it:
1890-12-10T05:48:40

Putting aside that I don't understand your expectation on dates before 1970 you can use FORMAT_DATETIME function to format your date result as follow:
SELECT FORMAT_DATETIME("%F %T", EXTRACT(DATETIME FROM TIMESTAMP_MILLIS(-2494865480000)))
The result of this SQL is
1890-12-10 05:48:40

Related

How to convert Epoch to date in athena?

I have written a query to convert epoch to date conversion in Athena but the result is not expected
select from_unixtime(cast(epoch_values as bigint))as dates from mybdbase
result is :
dates
+54113-07-13 10:11:53.000
+54113-07-13 10:11:57.000
The year is shown in the above table, How to solve this?
Use this:
from_unixtime(event_timestamp/1000000)
You could validate the results with this page:
https://www.epochconverter.com/

Converting timestamp on whole table in bigquery

I have this table which stores millions of rows of data. This data has a date that indicates when was the data entered. I store the data in NUMERIC schemas with EPOCH UNIX as the format. However, I wanted to convert them to human date (yyyy-mm-dd hh:mm:ss) and later sort them by date not queried date.
However, it took me so long to find a suitable way. Here's my attempt.
I used SELECT CAST(DATE(timestamp) AS DATE) AS CURR_DT FROM dataset.table but it gave me this error:
No matching signature for function DATE for argument types: NUMERIC. Supported signatures: DATE(TIMESTAMP, [STRING]); DATE(DATETIME); DATE(INT64, INT64, INT64) at [1:13]
I used this method BigQuery: convert epoch to TIMESTAMP but still didn't fully understand
I'm a novice in coding so I hope you guys understand the situation. Thanks!
If I am understanding your question correctly you would like to take a numeric EPOCH time that is stored as an integer and convert it to a timestamp?
If so you can use the following in BigQuery Standard SQL:
select TIMESTAMP_SECONDS(1606048220)
It gives the output of:
2020-11-22 12:30:20 UTC
Documentation
If you only want the date component, then you would convert to a date after converting to a timestamp. Presumably you have seconds, so you would use TIMESTAMP_SECONDS() -- but there are similar functions for milliseconds and microseconds.
For just the date:
select date(timestamp_seconds(col))
Note that this removes the time component.

How to convert UNIX epoch days to DATE in Snowflake?

I have some values which correspond to the number of days since Epoch and want to store them in DATE column in Snowflake.
Example:
33057
Corresponds to Sunday, July 4, 2060
Would be really helpful if someone can point me to the correct Snowflake documentation(if available out of the box) for such
I think this will work:
select dateadd(day, <your number> - 33057, '2060-07-04')
So, for the specific conversion of "33057", this would be:
select dateadd(day, 33057 - 33057, '2060-07-04')
But it should work for any integer.
TO_TIMESTAMP_x(numeric_expr) is what your looking for. And if you only want the date part I would then truncate via ::DATE
numeric_expr
A number of seconds (if scale = 0 or is absent) or fractions of a second (e.g. milliseconds or nanoseconds) since the start of the Unix epoch (1970-01-01 00:00:00 UTC). If a non-integer decimal expression is input, the scale of the result is inherited.
if you have milliseconds you can use the TO_TIMESTAMP_x(numeric_expr, scale) form
select to_timestamp(12334567) as t_from_s
,to_timestamp(12334567000, 3) as t_from_ms;
gives
T_FROM_S T_FROM_MS
1970-05-23 18:16:07.000 1970-05-23 18:16:07.000
We use the _NTZ variant for all our usages, as we have all data in UTC from the servers.

SAS timestamp from scientific notation to yyyy/mm/dd hh:mm:ss

Problem:
My timestamp is being displayed in scientific notation. I would like to display the column without scientific notation, and create a second column formatted as a long date, yyyy/mm/dd hh:mm:ss.
Steps taken:
I've already converted the column from a UNIX Epoch (1960) timestamp to SAS time (1970) timestamp. But scientific notation persists. I tried date20. doesn't do the trick, either.
Timestamp in Scientific Notation
My current insufficient code fails to format the timestamp column as a date.
proc print data=heart._23a;
format timestamp date9.;
run;
Results:
It results in no errors, but it redimensions my matrix to a 1x3. I need to obtain a matrix of the same dimension, just with a reformatted timestamp. I appreciate any help, but please keep it simple, I am in unknown territory!
datetime17. is the standard timestamp format in SAS, though you have many other choices as well. ymddttm. is the closest to what you're looking for, I believe.
One important distinction here: SAS has two concepts, date and datetime. date is number of days since 1/1/1960 and has no time part, while datetime is number of seconds since 1/1/1960 00:00:00 and has both time and date. You can use datepart to convert datetime -> date, or dhms to convert date -> datetime.
Your question also seems to get the two epochs backwards. UNIX epoch is 1970. SAS epoch is 1960.
Finally, if you want to display the raw number of seconds, use w.d format instead of bestw.d format - format timestampvar 14. for example, where w is number of characters (digits) wide total including decimal.

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 convert that Epoch timestamp to sql server timestamp.
I have 3 different columns with the same format. How can I change the values in those columns.
For Example:
Epoch timestamp ---1291388960
sql server timestamp--- 2010-12-03 15:09:20.000
I have 3 different columns with the same format. How can I change the values in those columns.
To update 3 columns in a table, you can pair DATEADD seconds to the epoch (1 Jan 1970) with the column name, i.e.
update tbl set
datetimecol1 = dateadd(s, epochcol1, '19700101'),
datetimecol2 = dateadd(s, epochcol2, '19700101'),
datetimecol3 = dateadd(s, epochcol3, '19700101')
You can't update in place since a bigint column cannot also be a datetime column. You have to update them into 3 other columns.
Use the DATEADD function:
SELECT DATEADD(ss, 1291388960, '19700101')
...specifying a date of January 1st, 1970. In this example, it was provided in the YYYYMMDD format.
DATEADD will return a DATETIME data type, so if you have a table & column established -- you can use the function to INSERT/UPDATE depending on your needs. Provide details, and I'll clarify. Once you have a DATETIME to work with, you can use CAST or CONVERT to format the date in TSQL.