Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert timestamp to date in Presto?

Tags:

sql

presto

trino

I like to convert my timestamp columns to date and time format. How should I write the query from presto? my timestamp is UTC time. Thank you very much

Timestamp format"1506929478589"
After query convert it looks like "2016-10-25 21:04:08.436"
like image 810
user8818601 Avatar asked Oct 23 '17 10:10

user8818601


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 I cast a timestamp to a date?

You can, however, cast a TIMESTAMP to a DATE and then cast the DATE to a CHAR of less than 24 characters. For example: SELECT CAST (CAST (timestamp_col AS DATE) AS CHAR(10)) FROM table1; CHAR data type into a DATE TIME or TIMESTAMP data type.


2 Answers

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

like image 130
Piotr Findeisen Avatar answered Sep 28 '22 10:09

Piotr Findeisen


You can use the date_format function (docs here: https://prestodb.io/docs/current/functions/datetime.html)

Here's an example:

date_format(charges.created, '%Y-%m') as rev_month

If for some reason you're comparing dates you don't need to do the conversion like that, you can do the following

where customers.created BETWEEN timestamp '2018-04-01 00:00:00.000' AND timestamp '2018-05-01 00:00:00.000' 
like image 37
DougB Avatar answered Sep 28 '22 11:09

DougB