在配置单元中获取子字符串

时间:2018-12-14 13:30:00

标签: hive hiveql

我正在尝试从Hive获取字符串的子字符串。我有一个这样的字符串:2017-06-05 09:06:32.0

我想要的是从小时中获取前两位数字,即09。 我用这个命令花了整整一个小时:

    for (const country of json) {
      if (country.eventIDs.length > 0) { 
          country.eventIDs.forEach(id => promises.push(getEventData(country, id))) 
      }
    }

命令的结果是:09:06:32.0

为了只获取09,请尝试以下命令:

SELECT SUBSTR(hora,11) AS subhoras FROM axmugbcn18.bbdd WHERE hora =  '2017-06-05 09:06:32.0'

但结果为空白。

如何只检索小时的两位数字?

谢谢

2 个答案:

答案 0 :(得分:2)

您可以通过几种方式hours值中提取 timestamp

1。使用子字符串功能:

select substring(string("2017-06-05 09:06:32.0"),12,2);
+------+--+
| _c0  |
+------+--+
| 09   |
+------+--+

2。使用Regexp_Extract:

select regexp_Extract(string("2017-06-05 09:06:32.0"),"\\s(\\d\\d)",1);
+------+--+
| _c0  |
+------+--+
| 09   |
+------+--+

3。使用时间:

select hour(timestamp("2017-06-05 09:06:32.0"));
+------+--+
| _c0  |
+------+--+
| 9    |
+------+--+

4。使用from_unixtime:

select from_unixtime(unix_timestamp('2017-06-05 09:06:32.0'),'HH');
+------+--+
| _c0  |
+------+--+
| 09   |
+------+--+

5。使用date_format:

select date_format(string('2017-06-05 09:06:32.0'),'hh');
+------+--+
| _c0  |
+------+--+
| 09   |
+------+--+

6。使用拆分:

select split(split(string('2017-06-05 09:06:32.0'),' ')[1],':')[0];
+------+--+
| _c0  |
+------+--+
| 09   |
+------+--+

答案 1 :(得分:0)

请尝试以下操作:

select 
'2017-06-05 09:06:32.0' as t, 
hour('2017-06-05 09:06:32.0'),                               -- output: 9
from_unixtime(unix_timestamp('2017-06-05 09:06:32.0'),'HH')  -- output: 09
from table_name;

您可以尝试hourunixtimestamp以获得所需的结果。

希望这会有所帮助:)

相关问题