If I understand correctly, you are trying to convert a string representing a given date to another type.
Note: (as @Samson Scharfrichter mentioned)
- default date representation - ISO8601
- date is stored in binary format (not as a string)
There are several ways to do this. And you are close to a solution. I would use CAST (which translates to DATE_TYPE):
SELECT cast('2018-06-05' as date);
Result: 2018-06-05 DATE_TYPE
or (depending on your template)
select cast(to_date(from_unixtime(unix_timestamp('05-06-2018', 'dd-MM-yyyy'))) as date)
Result: 2018-06-05 DATE_TYPE
And if you decide to convert ISO8601 to a date type:
select cast(to_date(from_unixtime(unix_timestamp(regexp_replace('2018-06-05T08:02:59Z', 'T',' ')))) as date);
Result: 2018-06-05 DATE_TYPE
Hive has its own functions, I wrote several examples to illustrate these date- and cast- functions:
Examples of date and time functions:
Convert string / timestamp / date to date
SELECT cast(date_format('2018-06-05 15:25:42.23','yyyy-MM-dd') as date);
Convert string / timestamp / date to BIGINT_TYPE
SELECT to_unix_timestamp('2018/06/05 15:25:42.23','yyyy/MM/dd HH:mm:ss'); -- 1528205142 BIGINT_TYPE SELECT to_unix_timestamp(current_date(),'yyyy/MM/dd HH:mm:ss'); -- 1528205000 BIGINT_TYPE SELECT to_unix_timestamp(current_timestamp(),'yyyy/MM/dd HH:mm:ss'); -- 1528205142 BIGINT_TYPE
Convert string / timestamp / date to string
SELECT date_format('2018-06-05 15:25:42.23','yyyy-MM-dd');
Convert BIGINT unixtime to STRING
SELECT to_date(from_unixtime(unixtime,'yyyy/MM/dd HH:mm:ss'));
Convert string to BIGINT unixtime
SELECT unix_timestamp('2018-06-05 15:25:42.23','yyyy-MM-dd') as TIMESTAMP;
Convert string to TIMESTAMP
SELECT cast(unix_timestamp('2018-06-05 15:25:42.23','yyyy-MM-dd') as TIMESTAMP);
Idempotent (String → String)
SELECT from_unixtime(to_unix_timestamp('2018/06/05 15:25:42.23','yyyy/MM/dd HH:mm:ss'));
Idempotent (Date → Date)
SELECT cast(current_date() as date);
Current Date / Timestamp
SELECT current_date();