我才刚刚开始学习有关HiveQL或SQL的更多信息。 我是新手,这是我在SOF中的第一个问题。 问题
我的来源有一个带有多种格式的时间戳字段。 是yy-dd-mm hh.mm.ss.sssssss,mm-dd-yy hh.mm.ss.sssssss,dd-mm-yyyy hh:mm:ss:sssssss等,但是我想要我的蜂巢表格时间列采用Timestamp FF6格式,即YYYY-DD-MM HH:MM:SS.SSSSSS。完全像这样在秒点(。)和6位小数之后。
那么最好的方法是什么?编写UDF或Reg_exp更好吗?请通过示例代码为我提供所有可能的答案。将会非常有帮助。预先感谢。
答案 0 :(得分:0)
Samson Scharfrichter嗨,
所以我使用了以下正则表达式
选择 案件 当time_problem RLIKE'[0-9] {4}-[0-9] {2}-[0-9] {2} [0-9] {2}:[0-9] {2}:[0 -9] {2}。[0-9] {6}'然后是CAST(REGEXP_REPLACE(time_problem,'([0-9] {4})-([0-9] {2})-([0- 9] {2})([0-9] {2}):([0-9] {2}):([0-9] {2})。([0-9] {6})' ,'$ 1- $ 2- $ 3 $ 4:$ 5:$ 6. $ 7')作为时间戳) 当time_problem RLIKE'[0-9] {4} / [0-9] {2} / [0-9] {2} [0-9] {2}:[0-9] {2}:[0 -9] {2}:[0-9] {6}'然后是CAST(REGEXP_REPLACE(time_problem,'([0-9] {4})/([0-9] {2})/([0- 9] {2})([0-9] {2}):([0-9] {2}):([0-9] {2}):([0-9] {6})' ,'$ 1- $ 2- $ 3 $ 4:$ 5:$ 6. $ 7')作为时间戳) 当time_problem RLIKE'[0-9] {2}-[0-9] {2}-[0-9] {4} [0-9] {2}:[0-9] {2}:[0 -9] {2}:[0-9] {6}'然后是CAST(REGEXP_REPLACE(time_problem,'([0-9] {2})-([0-9] {2})-([0- 9] {4})([0-9] {2}):([0-9] {2}):([0-9] {2}):([0-9] {6})' ,'$ 3- $ 1- $ 2 $ 4:$ 5:$ 6. $ 7')作为时间戳) ELSE CAST(0时间戳) END AS time_problem_in_timestamp 从 mysample;
它适用于大多数情况。但是,如果我得到空日期,它将显示0。我想对给定日期进行硬编码。
我尝试了以下代码 选择 案件 当test_time RLIKE'[0-9] {4}-[0-9] {2}-[0-9] {2} [0-9] {2}:[0-9] {2}:[0 -9] {2}。[0-9] {6}'然后是CAST(REGEXP_REPLACE(test_time,'([0-9] {4})-([0-9] {2})-([0- 9] {2})([0-9] {2}):([0-9] {2}):([0-9] {2})。([0-9] {6})' ,'$ 1- $ 2- $ 3 $ 4:$ 5:$ 6. $ 7')作为时间戳) 当test_time RLIKE'[0-9] {4} / [0-9] {2} / [0-9] {2} [0-9] {2}:[0-9] {2}:[0 -9] {2}:[0-9] {6}'THEN CAST(REGEXP_REPLACE(test_time,'([0-9] {4})/([0-9] {2})/([0- 9] {2})([0-9] {2}):([0-9] {2}):([0-9] {2}):([0-9] {6})' ,'$ 1- $ 2- $ 3 $ 4:$ 5:$ 6. $ 7')作为时间戳) 当test_time RLIKE'[0-9] {2}-[0-9] {2}-[0-9] {4} [0-9] {2}:[0-9] {2}:[0 -9] {2}:[0-9] {6}'THEN CAST(REGEXP_REPLACE(test_time,'([0-9] {2})-([0-9] {2})-([0- 9] {4})([0-9] {2}):([0-9] {2}):([0-9] {2}):([0-9] {6})' ,'$ 3- $ 1- $ 2 $ 4:$ 5:$ 6. $ 7')作为时间戳) When test_time RLIKE''THEN CAST(REGEXP_REPLACE(test_time,'2018-10-22 10:12:24.123456')) ELSE CAST(0时间戳) END AS test_time_in_timestamp 从 mytest;
我遇到错误。我该如何解决。有输入吗?