问题描述
我正在尝试删除HIVE中字符串的一部分.我想删除一列中所有记录的最后十一个字符.数据如下:
I am trying to delete a part of a string in HIVE. I want to delete the last eleven characters for all records in a column. The data looks like:
我希望它看起来像:
我尝试过的代码如下:
选择右(a.ord_id,len(a.ord_id)-ll)
Select right(a.ord_id, len(a.ord_id)-ll)
它不起作用,因为len在HIVE中不起作用
It isn't working because len isnt a function in HIVE
我遇到的另一个问题是某些记录已经采用了正确的格式.这是否意味着我需要创建一个用于对此进行检查的case语句?
Another issue I have is that some of the records are already in the correct format. Does this mean I need to create a case statement that checks for this?
推荐答案
您可以使用regexp提取|
字符之前的数字:
You can extract digits before |
character using regexp:
hive> select regexp_extract('1018492743|0001-01-01','([0-9]*)\\|',1);
OK
1018492743
或使用substr获取前10个字符:
Or use substr to get first 10 characters:
hive> select substr('1018492743|0001-01-01',1,10);
OK
1018492743
或者完全像您描述的使用length和substr获取不带最后11个字符的子字符串一样:
Or exactly like you described using length and substr to get substring without last 11 characters:
hive> select substr('1018492743|0001-01-01',1,length('1018492743|0001-01-01')-11);
OK
1018492743
使用split()
的另一种解决方案:
One more solution using split()
:
hive> select split('1018492743|0001-01-01','\\|')[0];
OK
1018492743
在此处查看文档: https://cwiki.apache.org/confluence /display/Hive/LanguageManual + UDF
这篇关于删除HIVE中的一部分字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!