Amazon Athena unable to convert date to timestamp in query - sql

I would like to convert date value to timestamp format using Amazon Athena Query and used below function to convert ,able to convert some of the dates into timestamp format but not working for some of the date values ,Please find below example
date_parse('10/20/1977','%m/%d/%Y') output is 1977-10-20 00:00:00:000
But for below type of dates showing error:
date_parse('02/29/1977''%m/%d/%Y') output is
cannot parse "02/29/1977":value 29 for dayOfMonth must be in range [1-28]

1977 (Gregorian calendar) was not a leap year, so there were no 29th of February - there were only 28 days. So you need to fix the data.

Related

How to insert DD MON YYYY format value into a date column in a snowflake table

My csv file has a date column having values in DD MON YYYY format eg: 28 Nov 2022.
When i tried inserting it into a date column(datatype= DATE) it is showing the below error. I have also tried using TO_DATE , TO_VARCHAR but getting the same error.
Kindly help me to resolve this.
Error: Date '28 Nov 2022' is not recognized
I want to insert the value in the same format (DD MON YYYY) into a column of date data type ,without changing the format i.e '28 Nov 2022'.
I was reading documentation (https://docs.snowflake.com/en/sql-reference/data-types-datetime.html#date) and i read: "DATE accepts dates in the most common forms (YYYY-MM-DD, DD-MON-YYYY, etc.)."
So i think the format you are trying to write is a no-supported date format.
you can:
format your date in a supported date format before write field in db.
write in a varchar datatype field, but in this case you'll lose all tools on date type.
I don't see other ways!

Convert date to dateTtime format in SQL

I am trying to convert a date column (ie. 2012-10-02) to the first day of the year with time (ie. 2012-01-01T00:00:00) in sql.
Is there a way to do so in the SELECT query?
for BigQuery use below
select timestamp_trunc('2012-10-02', year)
with output
2012-01-01 00:00:00 UTC
Note - if you column is of date type - the output will be
2012-01-01T00:00:00
and finally, you can use datetime_trunc instead of timestamp_trunc and you will get expected result - 2012-01-01T00:00:00
Look at the YEAR() function.
It would allow you to extract just the year, and then just as the date and time you need.

AWS Athena (Trino SQL) Convert birthdate string (mm/dd/yy) to date -- need twentieth century

AWS Athena (Trino) Convert birthdate string (mm/dd/yy) to date -- need twentieth century
I have found similar questions, but they either aren't specific to Athena, or to this two-digit to date format for a birthdate (e.g. 56 should map to 1956)
An example of similar question (this one is 4-year date is):
Amazon Athena Convert String to Date
For example, cast( date_parse(trim("date of birth"),'%m/%d/%Y') as date ) as our_date_of_birth gives the laughable: 0094-01-04 instead of a date in 1994
However, if I use:
cast( date_parse(trim("date of birth"),'%m/%d/%y') as date ) as our_date_of_birth,
it sometimes gives me correct date, but sometimes something like: 2062-07-31 instead of 1962
Finally, just plain:
cast( trim("date of birth") as date ) as our_date_of_birth
gives an error: INVALID_CAST_ARGUMENT: Value cannot be cast to date: 10/11/78
Is there a way to get twentieth century birthdates from these casts in Athena Trino?
Obviously there would be edge cases such as 01/01/20 which could map to either 1920 or 2020, but dates like 01/01/50 should definitely map to 1950.
Sample data and outputs:
01/01/56 -- output would be 1956-01-01 as date
01/01/08 -- output would be 2008-01-01 as date
01/01/21 -- output would be 2021-01-01 as date (* some would want 1921 here)
07/01/21 -- output would be 1921-07-01 as date (since as of posting 07/01/2021 would be in future)
**The outuput format isn't crucial, it could be 01/01/1956, just so it is a true 'date' in Athena Trino.**
One way would be to subtract 100 years whenever the parsed date is in the future. For example:
select case when
parse_datetime(birthdate, 'MM/dd/yy') > current_timestamp then
parse_datetime(birthdate, 'MM/dd/yy') - interval '100' year
else parse_datetime(birthdate, 'MM/dd/yy')
end as birthdate
Note that this would work only until the next century.
The parse_datetime function returns a timestamp object, see the docs: https://prestodb.io/docs/current/functions/datetime.html
parse_datetime uses java's DateFormat conventions. From the docs:
https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
For parsing with the abbreviated year pattern ("y" or "yy"), SimpleDateFormat must interpret the abbreviated year relative to some century. It does this by adjusting dates to be within 80 years before and 20 years after the time the SimpleDateFormat instance is created.

Date_Trunc and To_Date Questions SQL

Can we use date_trunc for a date (not date-time) that we are trying to "truncate" (not sure if the term can be applied here) to e.g. the start of the week? So if I have date_trunc(week, 28/10/2020) and I want to get the start of the week that 28th of October lies in (so 26th of October)? I tried this in my SQL command line but I get error messages.
If I am doing: SELECT to_date ('02 Oct 2001', 'DD Mon YYYY'); How can I ensure the resulting format is in a date format I specify (rather than the default date format)? For example if I want it in format DD-MM-YYYY?
select to_char(date '2017-06-02', 'MM') < in this example, why do we need "date" for this to work? The general format for to_char should be TO_CHAR (timestamp_expression, 'format'). I don't see in this general format that we need "day".
if I have a WHERE filter like to_char(order_date, '20-10-2020'), and there are indeed some rows with this order date, will these rows still show in my results (after running query) if these rows are in DATE format (so 20 Oct is in date format) as opposed to string (which is what I am filtering by as I am doing to_char). I know there would be no need to use to_char in this case but just asking..
yes, you can use date in text form but you have to cast it to a correct type
these queries will work
select date_trunc('week', '2020-10-28'::date);
select date_trunc('week', '10/28/2020'::date);
-- as well as
select date_trunc('week', '2020-10-28'::datetime);
and return timestamp 2020-10-26 00:00:00.000000
note, next query
select date_trunc('week', '28/10/2020'::date);
will fail with error date/time field value out of range: "28/10/2020";
You can use to_char, it returns text, if you need a date format you have to case it again
select to_char( to_date ('02 Oct 2001', 'DD Mon YYYY'), 'DD-MM-YYYY')::date;
select to_char('02 Oct 2001'::date, 'DD-MM-YYYY')::date;
'2017-06-02' is a text and it can't be automatically converted to timestamp. Actually I don't know a text format which can.
No, you need to explicitly cast into date type to use it as a filter
where order_date = date_stored_as_a_text::date
I am answering the questions in a different order as there are some wrong assumptions:
Question 3
There is a general difference between '2017-06-02' and date '2017-06-02' - the first one is just a varchar, a string, NOT handled as a date by Redshift, the 2nd one tells Redshift to handle the string as date and therefore works.
Question 2
A date data type column has no format - you may an sql client that can display date columns in different formats, however, this is not a functionality of redshift. SELECT to_date ('02 Oct 2001', 'DD Mon YYYY'); tells redshift to convert the string '02 Oct 2001' to date.
Question 1
DATE_TRUNC('datepart', timestamp) also supports week as datepart - see Date parts for date or timestamp function (Also shown in the example of AWS). You should also be able to provide a date instead of a timestamp.
Question 4
to_char(order_date, '20-10-2020')is not a filter and you are using it wrong.
AWS TO_CHAR
TO_CHAR converts a timestamp or numeric expression to a character-string data format.
I guess you are rather looking for:
where to_char(order_date, 'YYYY-MM-DD') = '20-10-2020'

hive date cast issue

Hi in my Hive table I have a column with the date values like this .
cl1
31102019000000
30112019000000
31122019000000
I have tried to convert the column values to date format like this
Select from_unixtime(unix_timestamp(cl1,'yyyy/MM/dd'),'yyyy-MM-dd') from table1;
it prints NUll. Any help will be appreciated.
You said you have dates in dd-mm-yyyy but then posted data that doesn't have any hyphens in at all
Assuming 31102019000000 is 31-oct-2019 00:00:00
Select from_unixtime(unix_timestamp(cl1,'ddMMyyyyHHmmss'),'yyyy-MM-dd') from ...
Match the format string to the data..
Try using this
Select from_unixtime(unix_timestamp(REGEXP_REPLACE(cl1,'0+$',''),'ddMMyyyy'),'yyyy-MM-dd') from table1;
but this would fail for years 2020,2010.
So the below query would be a better alternative
Select from_unixtime(unix_timestamp(cast(cast(cl1/1000000 as bigint) as string),'ddMMyyyy'),'yyyy-MM-dd') from table1;
The UnixTimeStamp which you are trying to convert to date format is out of range.
Your Time stamp corresponds to :
GMT: Saturday, August 2, 2955 1:43:20 AM
Your time zone: Saturday, August 2, 2955 5:43:20 AM GMT+04:00
Relative: In 936 years
It is an open bug in MYSql community. Please check the below URL for reference.
https://forums.mysql.com/read.php?20,385047,385132#msg-385132
MySQL database's built-in functions like UNIX_TIMESTAMP() will return 0 after 03:14:07 UTC on 19 January 2038. (Source: https://en.wikipedia.org/wiki/Year_2038_problem)