converting Athena timestamp to date - sql

I am running a query against Athena, and it breaks. Specifically, I get an error for the below fragment:
avg(
DATE_DIFF(
'minute',
CAST(from_iso8601_timestamp("sessions_staging".session_start_at) AS TIMESTAMP),
CASE
WHEN CAST("sessions_staging__end_raw" AS TIMESTAMP) + INTERVAL '1' MINUTE > CAST("sessions_staging".next_session_start_at AS TIMESTAMP) THEN CAST("sessions_staging".next_session_start_at AS TIMESTAMP)
ELSE CAST("sessions_staging__end_raw" AS TIMESTAMP) + INTERVAL '30' MINUTE
END
)
) "sessions_staging__average_duration_minutes"
Athena complains with Value cannot be cast to timestamp: 2022-08-03T00:05:54.300Z.
I tried a bunch of tricks like casting my date to string then casting again to a time or a timestamp type. A similar problem caused by the same issue is covered some in converting to timestamp with time zone failed on Athena
The value seems to be just fine. I am able to execute: SELECT CAST(From_iso8601_timestamp('2022-08-03T00:05:54.300Z') AS timestamp). If I do not use CAST() and just do: "sessions_staging".session_start_at, it says that (varchar(6), varchar, timestamp) for function date_diff so I know that session_start_at is perceived as VARCHAR.
However, for the type of casting described as a solution to my issue to work, in the linked discussion, SELECT need to be used, it seems. Everything that I tried including string manipulations did not work.
How could I re-write my query/casts for Athena to process my request?

I ended up with:
CAST(DATE_PARSE(my_varchar_date, '%Y-%m-%dT%H:%i:%s.%f%z') AS TIMESTAMP)

Related

How to work with time interval in postgreSQL

I have a postgreSQL query that returns 'Late' when the given condition is met, currently, When i run below query I'm getting an error
SELECT
CASE WHEN CAST(so.scheduled_delivery_time AS date) < CURRENT_DATE OR CAST(so.scheduled_delivery_time AS date) = CURRENT_DATE AND DATETIME(so.scheduled_delivery_time) < DATETIME_SUB(datetime(current_datetime()), INTERVAL '3' HOUR) THEN 'Late'
END AS status
FROM
table1
Error
ERROR: function datetime(timestamp without time zone) does not exist Hint: No function matches the given name and argument types.
What I'm I missing?
Hey there — the simplest answer is that there is no function named DATETIME. Also it looks like your values are already timestamps, so you can probably just do this:
so.scheduled_delivery_time < current_datetime() - INTERVAL '3' HOUR
A good starting point to find the available functions, is the fine manual. CURRENT_TIMESTAMP will give you the current timestamp and you can use it like this:
SELECT CURRENT_TIMESTAMP;

How to convert BIGINT to DATE in Redshift?

I am trying to figure out how to convert and format a BIGINT field (i.e. 20200301) to a DATE type field using Redshift SQL. I was successful in getting the snippet below to work but I believe that returns a string and I need a valid date returned in 'YYYY-MM-DD' format. I've tried several other version unsuccessfully. Thank you in advance.
'''to_char(to_date(date_column::text, 'yyyymmdd'), 'yyyy-mm-dd')'''
You just want the to_date() part:
select to_date(date_column::text, 'YYYYMMDD')
When it is a timestamp we need the below code to convert into correct value.
select trunc(TIMESTAMP 'epoch' + date_column / 1000 * INTERVAL '1 second')

How to do timestamp conversion where the time zone value comes from another column [duplicate]

Here is an operation you can perform in Athena-
SELECT date_utc AT TIME ZONE 'America/Chicago'
FROM
(
SELECT TIMESTAMP '2018-09-09 12:00:00' as date_utc
) x;
In other sql engines you can change America/Chicago to a column-
SELECT date_utc AT TIME ZONE x.timezone
FROM
(
SELECT
TIMESTAMP '2018-09-09 12:00:00' as date_utc,
'America/Chicago' as timezone
) x;
In Athena you get-
line 1:30: no viable alternative at input 'time zone x'
Should it be possible to use x.timezone in Athena? This seems like a bug.
It indeed looks like a bug in the engine. What is interesting however is, that the underlying function works with a column parameter. So you can use this as a workaround.
SELECT at_timezone(date_utc,x.timezone)
FROM
(
SELECT
TIMESTAMP '2018-09-09 12:00:00' as date_utc,
'America/Chicago' as timezone
) x;
Adding some additional context to the the correct answer above:
AT_TIMEZONE function is not documented in presto docs. When someone uses timestamp AT time zone tz , its actually AT_TIMEZONE function that gets called. It appears to be an internal function, which some folks were already using. Here is an issue discussing it: https://github.com/prestodb/presto/issues/5162
The syntax appears to be AT_TIMEZONE(date_field, tz_field_or_string)

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);

HSQLDB just time value in insert

We are using HSQLDB for JUnits, our production database is Oracle.
I have a table in which there is a field (type is timestamp in Oracle) that carries the information about some important time in day.
I wanted to prepare test data for JUnit in script, but I failed.
The basic idea I can do in Oracle DB is
SYSDATE - TRUNC(SYSDATE)
when I tried the same with CURRENT_TIME in HSQLDB I got error:
org.hsqldb.HsqlException: incompatible data type in conversion
the next idea I got was to add time to "empty date", tried:
DATE '0000-01-01'
but got
org.hsqldb.HsqlException: data exception: invalid datetime format
strange for DATE '2013-01-01' it works fine, maybe the initialia zeros are the problem...no they are not, same for DATE '0-01-01'...
In this phase I was kind of mad already (think I can do in Oracle in a second and I spent several hours here).
From documentation I tried to find some combination of functions that creates the required result, but unfortunately I found that the documentation is not clear. It seems so for first read, but when tried I was surprised why this
DATEADD( 'hour', 1, CURRENT_DATE )
works fine, while
DATEADD( 'hour', 1, CURRENT_TIME)
ends with
org.hsqldb.HsqlException: incompatible data type in conversion
as opposite, DATE_ADD works for both and parameter type for both functions is the same in documentation :-/
I believe that I can do, something like
// 4x call of DATEADD, datetime not working as described above
date - years - months - days + seconds_from_midnight
but there have to be something simple to use (I hope so).
HSQLDB version: 2.3.1
Use this setting for your tests:
SET DATABASE SQL SYNTAX ORA TRUE
Then this works and returns an INTERVAL
SYSDATE - TRUNC(SYSDATE)
The simpler form is CAST (SYSDATE AS TIME) to return the time part of SYSDATE, which you can convert to an interval.
This date DATE '0000-01-01' is not accepted as there is no YEAR 0. You need to use DATE '0001-01-01'
This works fine to convert the time of the day into an INTERVAL:
LOCALTIME - TIME'00:00:00'
Note LOCALTIME has no time zone, while CURRENT_TIME has.