Find function in HIVE

◇◆丶佛笑我妖孽 提交于 2019-12-11 10:01:34

问题


I want to check if a field contains a string.

I want a function that would look like this:

FIND("string_to_find",field_to_search)

My data looks like this:

field_to_search
---------------
"no match in this string"
"record 2 has no matches"
"ahh finally xxxstring_to_findxxx is here"

I am looking for a function that identifies that the specified string is contained and at what position the string starts.

return
------
-1
-1
15

回答1:


The built in locate function does nearly exactly what you need except that for your input, it would return

return
------
0
0
16

Since it indexes from 1. So all you need to do is:

Select locate("string_to_find","ahh finally xxxstring_to_findxxx is here") -1; --returns 15
Select locate("string_to_find","foo") -1; --returns -1



回答2:


This is can be achieved by using instr and if in hive.

select if(instr(line,"xxxstring_to_findxxx")==0,-1,instr(line,"xxxstring_to_findxxx")) as position from find_tbl;  

where line is your column name



来源:https://stackoverflow.com/questions/31926318/find-function-in-hive

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!