hadoop 函数从sql Impala中的字符串中选择小时和分钟

mwngjboj  于 2023-04-29  发布在  Hadoop
关注(0)|答案(1)|浏览(268)

我的表有一个日期时间列,格式为“YYYY-MM-DD HH:MM:SS”。我想将此列拆分为年、月、日和时间的单独列。目前,我正在使用sql impala函数year()、month()和day()来分割日期组件,但在获取“hh:mm”格式的时间时遇到了问题。
我是这样做的:select year(cast(dt_tm_entry as timestamp)), month(cast(dt_tm_entry as timestamp)), day(cast(dt_tm_entry as timestamp)), --Hour and minute from dt_tm_entry from table
在文档中,我只找到了如何使用hour()和minute()分别选择hour和minute()。

wlzqhblo

wlzqhblo1#

由于您以字符串的形式开始时间戳,因此可以使用regexp_extract从字符串中提取每个组件。

select str_timestamp
, regexp_extract(str_timestamp, '(\\d{4})-(\\d{2})-(\\d{2}) (\\d{2}:\\d{2})', 0) as full_match
, regexp_extract(str_timestamp, '(\\d{4})-(\\d{2})-(\\d{2}) (\\d{2}:\\d{2})', 1) as year_extract
, regexp_extract(str_timestamp, '(\\d{4})-(\\d{2})-(\\d{2}) (\\d{2}:\\d{2})', 2) as month_extract
, regexp_extract(str_timestamp, '(\\d{4})-(\\d{2})-(\\d{2}) (\\d{2}:\\d{2})', 3) as day_extract
, regexp_extract(str_timestamp, '(\\d{4})-(\\d{2})-(\\d{2}) (\\d{2}:\\d{2})', 4) as time_extract
from times

您的另一个选择是像您一样提取小时和分钟,然后转换为string、lpad和concat。..

select concat_ws(':', lpad(cast(hour(record_created_timestamp) as string), 2, "0"), lpad(cast(minute(record_created_timestamp) as string), 2, "0"))

相关问题