Presto SQL - Converting a date string to date format

SqlPresto

Sql Problem Overview


I'm on presto and have a date formatted as varchar that looks like -

7/14/2015 8:22:39 AM

I've looked the presto docs and tried various things(cast, date_format, using split_part to parse and then cast) and am not getting this to convert to a date format that I can use with functions like date_diff.

I've tried:

cast(fieldname as timestamp)
date_format(fieldname, '%Y-%m-%d %T)

Both give me an error like this

'Value cannot be cast to timestamp: 3/31/2016 6:05:04 PM'

How do I convert this?

Sql Solutions


Solution 1 - Sql

I figured it out. The below works in converting it to a 24 hr date format.

select date_parse('7/22/2016 6:05:04 PM','%m/%d/%Y %h:%i:%s %p')

See date_parse documentation in Presto.

Solution 2 - Sql

You can also do something like this

date(cast('2016-03-22 15:19:34.0' as timestamp))

Solution 3 - Sql

Use: cast(date_parse(inv.date_created,'%Y-%m-%d %h24:%i:%s') as date)

Input: String timestamp

Output: date format 'yyyy-mm-dd'

Solution 4 - Sql

Converted DateID having date in Int format to date format: Presto Query

Select CAST(date_format(date_parse(cast(dateid as varchar(10)), '%Y%m%d'), '%Y/%m-%d') AS DATE)
from
	 Table_Name
limit 10;

Solution 5 - Sql

If your string is in ISO 8601 format, you can also use from_iso8601_timestamp

Solution 6 - Sql

    select date_format(date_parse(t.payDate,'%Y-%m-%d %H:%i:%S'),'%Y-%m-%d') as payDate 
    from testTable  t 
    where t.paydate is not null and t.paydate <> '';

Solution 7 - Sql

SQL 2003 standard defines the format as follows:

<unquoted timestamp string> ::= <unquoted date string> <space> <unquoted time string>
<date value> ::= <years value> <minus sign> <months value> <minus sign> <days value>
<time value> ::= <hours value> <colon> <minutes value> <colon> <seconds value>

There are some definitions in between that just link back to these, but in short YYYY-MM-DD HH:MM:SS with optional .mmm milliseconds is required to work on all SQL databases.

Solution 8 - Sql

date_format requires first argument as timestamp so not the best way to convert a string. Use date_parse instead.

Also, use %c for non zero-padded month, %e for non zero-padded day of the month and %Y for four digit year.

SELECT date_parse('7/22/2016 6:05:04 PM', '%c/%e/%Y %r')

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMoosaView Question on Stackoverflow
Solution 1 - SqlMoosaView Answer on Stackoverflow
Solution 2 - SqlRussell LegoView Answer on Stackoverflow
Solution 3 - SqlVINOD KUMAR KEDARNATHView Answer on Stackoverflow
Solution 4 - SqlRajiv SinghView Answer on Stackoverflow
Solution 5 - Sqlskeller88View Answer on Stackoverflow
Solution 6 - SqlIamnotmeView Answer on Stackoverflow
Solution 7 - SqlcoladictView Answer on Stackoverflow
Solution 8 - SqlJimson JamesView Answer on Stackoverflow