How to convert timestamp to date in Presto? - sql

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"

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

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'

Related

How to adjust timestamp timezone offset for YYYY-MM-DD''T''HH:mm:ss.SSSZ format timestamp in presto?

I have a dataset with timestamp strings like '2022-05-25T13:31:22.566-0400' I would like to convert it to '%Y-%m-%dT%H:%i:%s' format but adjusting for the timezone difference.
So for the above, how convert '2022-05-25T13:31:22.566-0400' to '2022-05-25T17:31:22.566' in Presto?
Thanks a lot!
You can use from_iso8601_timestamp to parse date, then cast to timestamp to remove timezone info (will be treated as UTC, at time zone 'UTC' should have the same effect) and use date_format to get required output format:
select date_format(
cast(from_iso8601_timestamp('2022-05-25T13:31:22.566-0400') as timestamp),
'%Y-%m-%dT%H:%i:%s')
Or
select date_format(
from_iso8601_timestamp('2022-05-25T13:31:22.566-0400') at time zone 'UTC',
'%Y-%m-%dT%H:%i:%s')
Output:
_col0
2022-05-25T17:31:22

How to convert a timestamp(yyyy-mm-dd-h-m-s to a date (yyyy-mm-dd)

I have a timestamp field that I need to drop the hrs,min,sec from so I can count by dates.
I've tried to_date, date_trunc, to_char
name.BIRTH_DT_TM = to_date(name.BIRTH_DT_TM,'DD mm YYYY HH24":"MI":"SS')
I would like name.BIRTH_DT_TM to show as YYYY-DD-MM without the Hours Minutes and Seconds
I had to use
cast(name.birth_dt_tm as date)
You should be able to use date_trunc():
select date_trunc('day', name.birth_dt_tm)

How to convert the date July 1, 2017 to dd-MM-yyyy using Hive SQL?

I have a Hive table with a Week column having values such as:
I have to convert this field to a date format such as: 2017-07-01 (yyyy-MM-dd) using hive SQL.
Any suggestions?
You can use a combination of from_unixtime and unix_timestamp.
select from_unixtime(unix_timestamp(weekCol,'MMM dd, yyyy'),'yyyy-MM-dd')
Use a combination of unix_timestamp and from_unixtime
select from_unixtime(unix_timestamp(week,'MMMM dd, yyyy'),'yyyy-MM-dd') from table_name;
unix_timestamp(string datetime, string pattern) converts datetime with given pattern to unix time stamp.
from_unixtime(bigint unixtime[, string format]) converts the number of seconds from unix epoch.

FROM UNIX TIME in Presto syntax

I'm currently trying to collect data that falls between 2 dates via Unix timestamp. All of our dates are stored as VARCHARs to the CAST function is used.
The line in my query reads as:
FROM_UNIXTIME(UNIX_TIMESTAMP(), '%Y %D %M %h:%i:%s %x') between
CAST(d.start_date AS TIMESTAMP) and CAST(d.end_date AS TIMESTAMP)
This returns as error:
Function unix_timestamp not registered
I also tried:
CAST(from_unixtime(unixtime) AS DATE) between
CAST(start_date AS DATE) and CAST(end_date AS DATE)
This produces the error:
Column unixtime cannot be resolved
Any suggestions?
Presto does not support unix_timestamp() function. You need to convert your varchar to date.
So:
now() BETWEEN
date_parse(start_date, '%Y-%m-%d %H:%i:%s') AND
date_parse(end_date, '%Y-%m-%d %H:%i:%s')
Adjust the date format string as per scenario.
For a full list of Presto date and time function, refer to: https://prestodb.io/docs/current/functions/datetime.html
I've used the code below to convert a unix timestamp to a date. You should then be able to compare it to the other two dates.
CAST(from_unixtime(unix_ts_col) AS DATE)
In the database I use, the unix timestamp has been stored as a string, so I had to cast it to an integer first.
CAST(from_unixtime(CAST(unix_ts_col AS INTEGER)) AS DATE);

SQL- Difference between TIMESTAMP, DATE AND TIMESTAMP WITH TIMEZONE?

What is the difference between TIMESTAMP , DATE AND TIMESTAMP with TIMEZONE?
E.g if I wanted to search for all entries between 01-JAN-1990 and 01-JAN-2000 , how would I do so in each format?
I have been searching for timestamp as:
SELECT COUNT(*) FROM TABLE_NAME WHERE DATE BETWEEN '01-JAN-1990' AND '01-JAN-2000;
But I am not sure what format to use to search for DATE or TIMESTAMP WITH TIMEZONE.
The data types and differences between them are in the documentation. The short version is:
DATE has precision down to a second with no time zone support;
TIMESTAMP has precision down to fractions of a second (up to nine decimal places, but your operating system affects that too), still with no time zone support;
TIMESTAMP WITH TIME ZONE has the same precision as TIMESTAMP but also has time zone support, as the name suggests;
TIMESTAMP WITH LOCAL TIME ZONE adjusts the stored value to and from the creating/querying session's local time zone.
You might find this article interesting too.
Whenever you are comparing datetime values stored in your database you should use values of the same datatype to compare against. You don't want to have to convert every value in the column for comparison, especially if the column is indexed. If you have a DATE column then compare with a DATE - don't compare as a string, and don't rely on implicit conversion of a string. When you do:
WHERE date_col BETWEEN '01-JAN-1990' AND '01-JAN-2000'
you are relying on your NLS_DATE_FORMAT being DD-MON-YYYY and your NLS_DATE_LANGUAGE being English. If someone else runs the same query in another session their settings may cause the query to fail (or in some cases, give wrong results, which can be worse). To avoid the language issue it's better to use month numbers rather than names. If you have a string variable to compare against you should use TO_DATE() to convert the string to a DATE using a fixed known format mask - don't rely on NLS. If you have a fixed value you can do the same, or you can use a date literal, which is shorter and unambiguous.
With the format you used you are also including any rows which have a the column set to midnight on January 1st 2000, but not any later on that day. That may be what you want, but make sure you understand how BETWEEN works. If you're actually looking for dates within that decade, including at any time on December 31st 1999, you can use:
WHERE date_col >= DATE '1990-01-01' AND date_col < DATE '2000-01-01'
For timestamps you can either use TO_TIMESTAMP() or a timestamp literal:
WHERE ts_col >= TIMESTAMP '1990-01-01 00:00:00'
AND ts_col < TIMESTAMP '2000-01-01 00:00:00'
For timestamps with time zones you can either use TO_TIMESTAMP_TZ() or a timestamp literal, with a names time zone region:
WHERE tstz_col >= TIMESTAMP '1990-01-01 00:00:00 America/New_York'
AND tstz_col < TIMESTAMP '2000-01-01 00:00:00 America/New_York'
Don't compare dates with strings. It can work if your session's nls_date_format happens to match the format of the string that you're using. But then your query will immediately fail for someone who has a different configuration. Compare dates with dates, timestamps with timestamps, etc.
For dates, you can use either ANSI date literals
SELECT COUNT(*)
FROM your_table
WHERE date_column BETWEEN date '1900-01-01' AND date '2000-01-01'
or you can use a to_date with an explicit format mask
SELECT COUNT(*)
FROM your_table
WHERE date_column BETWEEN to_date('1900-01-01', 'YYYY-MM-DD')
AND to_date('2000-01-01', 'YYYY-MM-DD')
Note that a date in Oracle always has a day and a time component. If you don't specify a time in your to_date, it will default to midnight. If you use an explicit to_date, you can use a string in any format just so long as it matches the format mask you pass in as the second parameter.
For timestamps, you can either use an ANSI timestamp literal
SELECT COUNT(*)
FROM your_table
WHERE timestamp_column BETWEEN timestamp '1900-01-01 00:00:00.000'
AND timestamp '2000-01-01 00:00:00.000'
or you can use a to_timestamp with an explicit format mask
SELECT COUNT(*)
FROM your_table
WHERE timestamp_column BETWEEN to_timestamp('1900-01-01 00:00:00.000', 'YYYY-MM-DD HH24:MI:SS.FFF')
AND to_timestamp('2000-01-01 00:00:00.000', 'YYYY-MM-DD HH24:MI:SS.FFF')
If you use an explicit to_timestamp, you can use a string in any format just so long as it matches the format mask you pass in as the second parameter.
For timestamps with time zone, as you may have guessed, you can either use an ANSI timestamp literal
SELECT COUNT(*)
FROM your_table
WHERE timestamp_column BETWEEN timestamp '1900-01-01 00:00:00.000 -05:00'
AND timestamp '2000-01-01 00:00:00.000 -05:00'
or you can use the to_timestamp_tz function with an explicit format mask
SELECT COUNT(*)
FROM your_table
WHERE timestamp_column BETWEEN to_timestamp('1900-01-01 00:00:00.000 -05:00', 'YYYY-MM-DD HH24:MI:SS.FFF TZH:TZM')
AND to_timestamp('2000-01-01 00:00:00.000 -05:00', 'YYYY-MM-DD HH24:MI:SS.FFF TZH:TZM')
If you use an explicit to_timestamp_tz, you can use a string in any format just so long as it matches the format mask you pass in as the second parameter.