数据准备
CREATE TABLE employee (
name STRING,
age INT
);
INSERT INTO employee VALUES
('Alice', 25),
('Bob', 30),
('Charlie', 35),
('David', 40);
方案一:like
select * from employee where name like '%i%';
方案二:locate(字符串,字段名)
select locate('aaa',field_name);
--locate
-- 返回某个字符串在某个字段中第一次出现的位置,如果要是没有找到,返回0
select * from employee where locate('i',name) !=0;
方案三:instr(字段名,字符串)
select instr(field_name,'aaa');
-- instr
-- 返回某个字符串在某个字段中第一次出现的位置,如果要是没有找到,返回0
-- 和 locate的区别是先写列名再写要查找的字符串
select *,instr(name,'i') from employee where instr(name,'i') !=0;
方案四:regexp_extract
可以使用regexp_extract(subject, pattern, index)函数来提取字符串中匹配指定正则表达式的字串。要判断一个字符串中是否包含字串"ABCD",可以使用如下代码:
regexp_extract(subject, 'ABCD', 0) != ''
select *,regexp_extract(name,'ice',0) from employee where regexp_extract(name,'ice',0) !='';
其中,subject是要判断的字符串,'ABCD;‘是要匹配的字串,0表示从整个匹配结果中获取整个字串。如果返回的字串不为空,则说明匹配到了,即包含字串'ABCD’,否则不包含。你需要将your_table替换为你实际使用的表名或子查询。
方案五:strpos [hive 不支持,其他技术支持 ]
strpos(string str, string substr)
str 是源字符串,substr 是要查找的字符串,查找子字符串在源字符串中第一次出现的位置,如果查找不到范围0 ,查找到了返回下标