Adding Hours to Dates in Presto

Adding Hours to Dates in Presto

In a Presto db table, I have two string fields, a date of the form, '2022-01-01', and an hour of the form, 22, for 22:00. I'm trying to combine these two elements into a proper timestamp, with date, hours, minutes, seconds. How can I accomplish this?

What I've tried so far

SELECT DATE_ADD('hour', 22, DATE(d) ) AS dt
FROM table

However, I get the error that 'hour' is not the proper DATE field. However, from documentation, my arguments appear to the in the proper order.

1 Answer

You need to either cast your date to timestamp:

-- sample data
WITH dataset(x, y) AS (
    VALUES (22,  '2022-01-01')
)

-- query
SELECT date_add('hour', x, cast(date(y) as timestamp))
FROM dataset

Or parse the string as timestamp:

SELECT date_add('hour', x, date_parse(y, '%Y-%m-%d'))
FROM dataset

Output:

_col0
2022-01-01 22:00:00.000

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

James H. Sterling
Author

James H. Sterling

James Sterling reports on renewable energy developments, climate policy, ecological conservation, and green tech innovations around the globe.