Amazon Athena Convert String to Date - amazon-web-services

I am looking to convert the following string: mmm-dd-yyyy to a date: yyyy-mm-dd
e.g
Nov-06-2015 to 2015-11-06
within Amazon Athena

I would do date_parse. Adjust your regex accordingly.
select date_parse('Nov-06-2015','%b-%d-%Y')
2015-11-06 00:00:00.000
refd:https://prestodb.io/docs/current/functions/datetime.html

You can also use cast function to get desire output as date type.
select cast(date_parse('Nov-06-2015','%M-%d-%Y') as date);
output--2015-11-06
in amazon athena
https://prestodb.io/docs/current/functions/datetime.html
used date parse to parse string and cast to convert 2015-11-06 00:00:00.000
into 2015-11-06

Related

Convert text column to timestamp in Amazon redshift

I have a text field "completed_on" with text values "Thu Jan 27 2022 11:55:12 GMT+0530 (India Standard Time)".
I need to convert this into timestamp.
I tried , cast(completed_on as timestamp) which should give me the timestamp but I am getting the following error in REDSHIFT
ERROR: Char/varchar value length exceeds limit for date/timestamp conversions
Since timestamps can be in many different formats, you need to tell Amazon Redshift how to interpret the string.
From TO_TIMESTAMP function - Amazon Redshift:
TO_TIMESTAMP converts a TIMESTAMP string to TIMESTAMPTZ.
select sysdate, to_timestamp(sysdate, 'YYYY-MM-DD HH24:MI:SS') as seconds;
timestamp | seconds
-------------------------- | ----------------------
2021-04-05 19:27:53.281812 | 2021-04-05 19:27:53+00
For formatting, see: Datetime format strings - Amazon Redshift.

Convert date isoString "2022-12-20T21:20:23.500Z" to format DD/MM/YYYY hh:mm in Athena sql

This is my actual date data
I would like to know if someone has the knowledge or knows how to format this date as mentioned in the title, I have already tried too many things, but I cannot solve it.
I would appreciate your help or contribution
2022-12-20T21:20:23.500Z -> 20/12/2022 21:20
Update:
if it helps anyone then i did this:
date_format( from_iso8601_timestamp('2009-10-30T17:59:55.047) at time zone 'UTC', '%d/%m/%Y %H:%i') as expiredate
It is ISO 8601 timestamp format.
Please first check data type then if it string you can convert it to date by
select from_iso8601_timestamp('2009-10-30T17:59:55.047') from mytable
https://queirozf.com/entries/presto-athena-examples-date-and-datetime-functions#convert-string-to-datetime-iso-8601-timestamp-format
Then you convert date datetype to any format you want

AWS Athena Query date

I have date in the following format in a file:
January 2, 2020 8:15:32 AM UTC
How to query it using Athena? I am trying to query all the rows where date is greater than a specific date.
I don't see above mentioned date format in the documentation.
As #John Rotenstein said you will have problens if your file row has columns split by , because your date columns also uses ,.
Nevertheless here is how you can cast your string January 2, 2020 8:15:32 AM UTC to date in Athena.
Athena uses presto in the backend and you can use the parse_datetime with the corresponding format to convert your string to a date.
parse_datetime(string, format) → timestamp with time zone
WITH t(x) AS (VALUES 'January 2, 2020 8:15:32 AM UTC')
SELECT parse_datetime(x, 'MMM d, YYYY hh:mm:ss a z')
FROM t;
produces the output
"_col0"
"2020-01-02 08:15:32.000 UTC"

Trim milliseconds part in Informatica

I have value in datetime format 2021-03-08 00:00:00.0 How to remove the milliseconds part from it?
Like: 2021-03-08 00:00:00.0 to 2021-03-08 00:00:00
if your target is a table/file and column is in datetime format, pls use this -
TO_DATE(TO_CHAR(dttm_col,'YYYY-MM-DD HH24:MI:SS'),'YYYY-MM-DD HH24:MI:SS')
if your target is a table/file and column is in string format, pls use this -
TO_CHAR(dttm_col,'YYYY-MM-DD HH24:MI:SS')
if your target is a file, its better to use string format. if its a table, you need to handle in database side.

Cast Date Column in the Redshift COPY Command

My data has the date in the "02JAN2020" format and I want to load the data using the COPY Command
copy test.Demographics from 's3://xyz-us-east-1/Blu/'
access_key_id ,’Access_Key_ID>’
secret_access_key ’<Secret_Access_Key>’
delimiter ',' dateformat 'auto'
GZIP;
The column data type is a date but it's still failing. I checked the stl error logs and it's the date formate issue.
I want the value in the column as 2020-01-02 and not 02Jan2020
Specify the date format with
DATEFORMAT 'DDMONYYYY'