将两列合并为一列并格式化内容以在 Hive 中形成准确的日期时间格式?
Merging two columns into a single column and formatting the content to form an accurate date-time format in Hive?
这些是 2 列(月、年)。我想从它们中创建一个具有准确日期时间格式 ('YYYY-MM-DD HH:MM:SS') 的列,并在 table.
中添加为新列
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
最好的配置单元查询是什么?我无法为 形成准确的正则表达式。
我假设 12 是月份和 3 day.In sql server 2008 试试这个
select convert(datetime,REPLACE(Month+'/'+year,'at',''))
我假设 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
年工作得很好,非常感谢@GoBrewers14。你救了我的命!
有点不匹配,这是正确的:
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
, ltrim(split(month, '\/')[0]) month
, split(year, ' ')[1] yr
, split(split(year, ' ')[3], '\:')[0] hour
, split(split(year, ' ')[3], '\:')[1] minutes
from db.table_name )
x ) y limit 5;
这些是 2 列(月、年)。我想从它们中创建一个具有准确日期时间格式 ('YYYY-MM-DD HH:MM:SS') 的列,并在 table.
中添加为新列 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
最好的配置单元查询是什么?我无法为 形成准确的正则表达式。
我假设 12 是月份和 3 day.In sql server 2008 试试这个
select convert(datetime,REPLACE(Month+'/'+year,'at',''))
我假设 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
年工作得很好,非常感谢@GoBrewers14。你救了我的命!
有点不匹配,这是正确的:
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
, ltrim(split(month, '\/')[0]) month
, split(year, ' ')[1] yr
, split(split(year, ' ')[3], '\:')[0] hour
, split(split(year, ' ')[3], '\:')[1] minutes
from db.table_name )
x ) y limit 5;