在配置单元中将日期格式 (04-10-2017) 转换为 (04-Oct-2017)
Convert Date Format (04-10-2017) to (04-Oct-2017) in hive
我有一个数据库,它的日期结果格式如下
20171004
所以我使用了这个查询
select SUBSTRING(week_first_day_id,1,4) ||'/' ||
(SUBSTRING(week_first_day_id,5,2)) || '/' ||
SUBSTRING(week_first_day_id,7,2) from d_date;
结果是
04/10/2017
我想将月份格式更改为 Mon ( 04/Oct/2017)
?
select date_format(week_first_day_id, 'dd/MMM/yyyy')
令我吃惊的是,您没有将日期存储为日期或标准格式。这是一件坏事,您应该修复数据。您可以解析日期并使用它。我认为代码如下所示:
select date_format(from_unix_timestamp(unix_timestamp(week_first_day_id, 'dd/mm/yyyy')))
使用unix_timestamp(string date, string pattern) 将给定的日期格式转换为自 1970-01-01 以来的秒数。然后使用 from_unixtime() 转换为所需格式:
hive> select from_unixtime(unix_timestamp( '20171004','yyyyMMdd'), 'dd-MMM-yyyy');
OK
_c0
04-Oct-2017
Time taken: 1.329 seconds, Fetched: 1 row(s)
我有一个数据库,它的日期结果格式如下
20171004
所以我使用了这个查询
select SUBSTRING(week_first_day_id,1,4) ||'/' ||
(SUBSTRING(week_first_day_id,5,2)) || '/' ||
SUBSTRING(week_first_day_id,7,2) from d_date;
结果是
04/10/2017
我想将月份格式更改为 Mon ( 04/Oct/2017)
?
select date_format(week_first_day_id, 'dd/MMM/yyyy')
令我吃惊的是,您没有将日期存储为日期或标准格式。这是一件坏事,您应该修复数据。您可以解析日期并使用它。我认为代码如下所示:
select date_format(from_unix_timestamp(unix_timestamp(week_first_day_id, 'dd/mm/yyyy')))
使用unix_timestamp(string date, string pattern) 将给定的日期格式转换为自 1970-01-01 以来的秒数。然后使用 from_unixtime() 转换为所需格式:
hive> select from_unixtime(unix_timestamp( '20171004','yyyyMMdd'), 'dd-MMM-yyyy');
OK
_c0
04-Oct-2017
Time taken: 1.329 seconds, Fetched: 1 row(s)