在Hive中判断字符串中是否包含指定子字符串

方法一 : like

1.1 传统的 like 查找:

select 'this 是 china' like('%是%');	--true
select 'this 是 china' like '%是%';		--true

select 'this 是 china' like('%否%');	--false
select 'this 是 china' like '%否%';		--false

1.2 基于正则表达式的查找:

参考java正则表达式的用法:

select 'this 是 china' rlike('是');		--true

select 'foobar' rlike('foo');			--true	
select 'foobar' rlike('bar');			--true
select 'foobar' rlike('^f.*r$');		--true

方法二 : locate

用法:

返回值函数名函数说明
intlocate(string substr, string str[, int pos])[一、参数说明: 参数1-substr: 待查找的字符子串; 参数2-str: 原始字符串; 参数3-pos: 指定位置,将查找在此位置及之后位置第一次出现指定字符串的位置.] …[英文说明: Returns the position of the first occurrence of substr in str after position pos.]

使用案例:

select locate('i','this is china'); 	--3
select locate('i','this is china',4);	--6
select locate('i','this is china',7);	--11

方法三 : regexp

用法:

运算符左右操作数类型函数说明
A REGEXP Bstrings属于关系运算符,同rlike函数, 根据java正则表达式判断. [一、参数说明: 参数1-A: 原始字符串; 参数2-B: 待比较的子字符串] …[英文说明:NULL if A or B is NULL, TRUE if any (possibly empty) substring of A matches the Java regular expression B, otherwise FALSE. For example, ‘foobar’ RLIKE ‘foo’ evaluates to TRUE and so does ‘foobar’ RLIKE ‘^f.*r$’.]
select 'this 是 china' regexp('是'); --true

--以下2种写法等价
select 'this 是 china' regexp('否'); --false
select 'this 是 china' regexp '否';  --false
  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值