Presto/SQL - Converting string timestamp to date throws error - sql

NOTE: I am running my query in Qubole's presto and sql command engine.
I am trying to convert my string timestamp to just date but none of the options are working out.
My string timestamp looks like 2017-03-29 10:32:28.0
and I want to have it like 2017-03-29
I have tried following queries to convert this string timestamp to retrieve date
1. select cast(created as date) from table1
Value cannot be cast to date: 2017-05-26 17:23:58.0
2. select cast(from_iso8601_timestamp(created) as date) from table1
Invalid format: "2014-12-19 06:06:36.0" is malformed at " 06:06:36.0"
3. select date(created) from table1
Value cannot be cast to date: 2012-10-24 13:50:00.0
How I can convert this timestamp to date in presto/sql?

As far as explained in the documentation, prestoDB seems to expect timestamps in a format '2001-08-22 03:04:05.321', and dates in a '2001-08-22'.
One solution would be to use a string function to extract the relevant part of the string before converting it. We know that the date part is located before the first space in the string, so.
If you need the date part as a string datatype:
split_part(created, ' ', 1)
If you need the date part as a date datatype:
cast(split_part(created, ' ', 1) as date)

You can try to use one of the following solutions:
SELECT
'2017-03-29 10:32:28.0' AS input_string,
DATE(date_parse('2017-03-29 10:32:28.0', '%Y-%m%-%d %H:%i:%s.%f')) AS solution_1,
DATE(try_cast('2017-03-29 10:32:28.0' as timestamp)) AS solution_2

Related

Presto SQL - Trouble with converting date in varchar to date format

I am having an issue converting a date column in varchar format to date format in presto-sql. The date_column is in this format : '9/5/2022', '12/23/2022', '%null%' etc.
I have tried the following queries:
SELECT
DATE_PARSE(date_column, '%Y-%m-%d')
FROM table
SELECT
CAST(date_column AS DATE)
FROM table
And there is always this error popping up:
Caused by: io.prestosql.jdbc.$internal.client.FailureInfo$FailureException: Invalid format: "11/30/2022" is malformed at "/30/2022"
The provided format (see the docs) represents year, month, day separated by - while your data is in month, day, year separated by /. Change the format accordingly:
SELECT DATE_PARSE('11/30/2022', '%m/%d/%Y');
Output:
_col0
-------------------------
2022-11-30 00:00:00.000
Note that strings like '%null%' (not just null strings) will be invalid also. If you really have those - you should consider wrapping the parse call into try.

Convert string of YYYYMMDD to YYYY-MM-DD Date format in Snowflake

Based on the example mentioned here in the Snowflake documentation, why are the date and timestamp values returning different values just by changing the ORDER BY clause? Also, I am trying to convert a string to a date format which is not returning correct results in Snowflake while this works fine in other SQL based Engines. Need help from experts on this.
This query
SELECT '20200710', TO_DATE('20200710');
is returning the following output
20200710 | 1970-08-22
Also tried:
SELECT TO_DATE('20200710', 'YYYY-MM-DD');
and got the error:
Can't parse '20200710' as date with format 'YYYY-MM-DD'
To convert to a date data type, you would use:
SELECT TO_DATE('20200710', 'YYYYMMDD')
I would recommend just keeping the date data type. But if you want a string in the format YYYY-MM-DD:
SELECT TO_CHAR(TO_DATE('20200710', 'YYYYMMDD'), 'YYYY-MM-DD')

change the date format in bigquery

I have a date column as dd-mm-yyyy. I would like to convert it to yyyy/mm/dd in bigquery.I have written the following query:
SELECT cast(format(Date, 'yyyy/mm/dd') as string) as Date FROM t1.
The error is : Too many arguments to FORMAT for pattern "23/04/2020"; Expected 1; Got 2.
Can you please assist.
First you need to parse date from dd-mm-yyyy string and then format it as yyyy/mm/dd as in below
FORMAT_DATE('%Y/%m/%d', PARSE_DATE('%d-%m-%Y', day))
You can test, play with above using dummy data as in below example
#standardSQL
WITH `project.dataset.table` AS (
SELECT '15-01-2020' day UNION ALL
SELECT '05-10-2019'
)
SELECT day, FORMAT_DATE('%Y/%m/%d', PARSE_DATE('%d-%m-%Y', day)) AS formated_day
FROM `project.dataset.table`
with output
Row day formated_day
1 15-01-2020 2020/01/15
2 05-10-2019 2019/10/05
You want FORMAT_DATE, not FORMAT.
SELECT FORMAT_DATE("%Y/%m/%d", DATE "2008-12-25");
The reason you're having trouble with FORMAT is that you gave it a format string that doesn't take any parameters. Seeing this, the engine barfs-"I don't need any more parameters to render this string."

Presto-Sql : Converting time in string format to date format

In presto, I have a date formatted as varchar that looks like below :
10:46:00
I need to cast this in timestamp. I have tried few but presto throwing errors as
Value cannot be cast to date:10:46:00 and Value cannot be cast to
timestamp:10:46:00
select cast('10:46:00' as DATE) from abc;
select cast('10:46:00' as TIMESTAMP) from abc;
Try with the below query it will solve your problem.
Input Query in Presto:
select (hour(date_parse(CheckStartTime,'%T')) + 1) as hr from TableName;
CheckStartTime:
Column name(varchar) of the table in the format of '12:32:20'.
Output:
13 (it will add one hour to the input time)

How to change date format in hive?

My table in hive has a filed of date in the format of '2016/06/01'. but i find that it is not in harmory with the format of '2016-06-01'.
They can not compare for instance.
Both of them are string .
So I want to know how to make them in harmory and can compare them. Or on the other hand, how to change the '2016/06/01' to '2016-06-01' so that them can compare.
Many thanks.
To convert date string from one format to another you have to use two date function of hive
unix_timestamp(string date, string pattern) convert time string
with given pattern to unix time stamp (in seconds), return 0 if
fail.
from_unixtime(bigint unixtime[, string format]) converts the
number of seconds from unix epoch (1970-01-01 00:00:00 UTC) to a
string representing the timestamp of that moment in the current
system time zone.
Using above two function you can achieve your desired result.
The sample input and output can be seen from below image:
The final query is
select from_unixtime(unix_timestamp('2016/06/01','yyyy/MM/dd'),'yyyy-MM-dd') from table1;
where table1 is the table name present in my hive database.
I hope this help you!!!
Let's say you have a column 'birth_day' in your table which is in your format,
you should use the following query to convert birth_day into the required format.
date_Format(birth_day, 'yyyy-MM-dd')
You can use it in a query in the following way
select * from yourtable
where
date_Format(birth_day, 'yyyy-MM-dd') = '2019-04-16';
Use :
unix_timestamp(DATE_COLUMN, string pattern)
The above command would help convert the date to unix timestamp format which you may format as you want using the Simple Date Function.
Date Function
cast(to_date(from_unixtime(unix_timestamp(yourdate , 'MM-dd-yyyy'))) as date)
here is my solution (for string to real Date type):
select to_date(replace('2000/01/01', '/', '-')) as dt ;
ps:to_date() returns Date type, this feature needs Hive 2.1+; before 2.1, it returns String.
ps2: hive to_date() function or date_format() function , or even cast() function, cannot regonise the 'yyyy/MM/dd' or 'yyyymmdd' format, which I think is so sad, and make me a little crazy.