Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting bigint to timestamp in presto

I have a column in my dataset that has a datatype of bigint:

Col1     Col2
   1     1519778444938790
   2     1520563808877450
   3     1519880608427160
   4     1520319586578960
   5     1519999133096120

How do I convert Col2 to the following format:

year-month-day hr:mm:ss

I am not sure what format my current column is in but I know that it is supposed to be a timestamp.

Any help will be great, thanks!

like image 348
nak5120 Avatar asked Apr 26 '18 19:04

nak5120


People also ask

How do I convert a timestamp to a Presto date?

You can convert timestamp to date with cast(col as date) or date(col) .

How do you convert Bigint to Pyspark timestamp?

You can use from_unixtime/to_timestamp function in spark to convert Bigint column to timestamp .

How do I change timestamp to date?

You can simply use the fromtimestamp function from the DateTime module to get a date from a UNIX timestamp. This function takes the timestamp as input and returns the corresponding DateTime object to timestamp.


1 Answers

Have you tried to use functions like from_unixtime? You could use it to convert unix time to timestamp, then you could use date_format to display it in way you want. Notice that in your example your unix time is with microseconds, so you might want to convert it first to milliseconds.

I have not tested that but I am assuming that your code should look like:

date_format(from_unixtime(col2/1000), '%Y-%m-%d %h:%i:%s')

Notice that from_unixtime accepts also a time zone.

Please visit this page to see the more details about date related functions: https://docs.starburstdata.com/latest/functions/datetime.html

like image 199
kokosing Avatar answered Sep 20 '22 14:09

kokosing