Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert a date format YYYY-MM-DD into integer YYYYMMDD in Presto/Hive?

How to CONVERT a date in format YYYY-MM-DD into integer YYYYMMDD in Presto/Hive?

I am trying to convert the below list into YYYYMMDD integers

WITH  all_dates  as (SELECT
    CAST(date_column AS DATE) date_column
FROM
    (VALUES
        (SEQUENCE(FROM_ISO8601_DATE('2017-07-01'),
                  FROM_ISO8601_DATE('2017-11-15'),
                  INTERVAL '1' DAY)
        )
    ) AS t1(date_array)
CROSS JOIN
    UNNEST(date_array) AS t2(date_column)
    )

I tried something like this but it doesn't work

   SELECT
   CAST(
      CAST(year(date_column) AS VARCHAR(4)) +
      right('0' + CAST(month(date_column) AS VARCHAR(2)), 2) +
      right('0' + CAST(day(date_column) AS VARCHAR(2)), 2) 
   AS DATETIME)
   FROM all_dates
like image 524
Chris Avatar asked Nov 15 '17 01:11

Chris


People also ask

How do I convert a date to Yyyymmdd?

Convert date to yyyy-mm-dd format with formula Select a blank cell next to your date, for instance. I1, and type this formula =TEXT(G1, "yyyy-mm-dd"), and press Enter key, then drag AutoFill handle over the cells needed this formula. Now all dates are converted to texts and shown as yyyy-mm-dd format.

How do I convert Yyyymmdd to hive?

Hive from_unixtime() is used to get Date and Timestamp in a default format yyyy-MM-dd HH:mm:ss from Unix epoch seconds. Specify the second argument in pattern format to return date and timestamp in a custom format.

How do I convert a timestamp to a Presto date?

You can convert timestamp to date with cast(col as date) or date(col) .


1 Answers

Also you can use date_format function:

hive> select cast(date_format('2017-07-01','yyyyMMdd') as int);
OK
20170701
like image 120
leftjoin Avatar answered Sep 20 '22 23:09

leftjoin