这些是 2 列(月,年)。我想从它们中创建一个具有准确日期时间格式('YYYY-MM-DD HH:MM:SS')的列,并将其添加为表中的新列。
Month year
12/ 3 2013 at 8:40pm
12/ 3 2013 at 8:39pm
12/ 3 2013 at 8:39pm
12/ 3 2013 at 8:38pm
12/ 3 2013 at 8:37pm
最好的 hive 查询可能是什么?我无法为 形成准确的正则表达式。
最佳答案
我假设 12 是 month
,而 3 是 day
,因为您没有指定。此外,您说您想要 HH:MM:SS
但您的示例中没有秒,所以我不知道您将如何将它们放在那里。我还在您的示例中将 8:37pm
更改为 8:37am
以尝试这两种情况。
查询:
select concat_ws(' ', concat_ws('-', yr, month, day)
, concat_ws(':', hour, minutes)) date_time
from (
select yr
, case when length(month) < 2 then concat('0', month) else month end as month
, case when length(day) < 2 then concat('0', day) else day end as day
, case when instr(minutes, 'pm') > 0 then cast(hour+12 as int)
when instr(minutes, 'am') > 0 and length(hour) < 2 then concat('0', hour)
else hour end as hour
, substr(minutes, 1, 2) minutes
from (
select ltrim(split(Month, '\\/')[1]) day
, split(Month, '\\/')[0] month
, split(year, ' ')[0] yr
, split(split(year, ' ')[2], '\\:')[0] hour
, split(split(year, ' ')[2], '\\:')[1] minutes
from test.sample_data ) x ) y
输出:
date_time
2013-12-03 20:40
2013-12-03 20:39
2013-12-03 20:39
2013-12-03 20:38
2013-12-03 08:37
关于sql - 将两列合并为一列并格式化内容以在 Hive 中形成准确的日期时间格式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30873875/