Presto SQL - Converting a date string to date format

后端 未结 8 1624
谎友^
谎友^ 2020-12-14 14:58

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

相关标签:
8条回答
  • 2020-12-14 15:32

    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.

    0 讨论(0)
  • 2020-12-14 15:33
        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 <> '';
    
    0 讨论(0)
  • 2020-12-14 15:33

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

    0 讨论(0)
  • 2020-12-14 15:36

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

    Input: String timestamp

    Output: date format 'yyyy-mm-dd'

    0 讨论(0)
  • 2020-12-14 15:36

    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;
    
    0 讨论(0)
  • 2020-12-14 15:40

    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')
    
    0 讨论(0)
提交回复
热议问题