Oracle INSTR 用法 Oracle 字符串搜索函数 INSTR Oracle instr 函数和like模糊查询对比
一、instr 函数
1、instr函数语法规则:instr(srcStr,targetStr ,[start_position],[num])
2、参数介绍:
- srcStr: 源字符串
- targetStr :目标字符串
- start_position:源字符串中开始的位置,默认从头开始。 【可选参数】
- num:目标字符串在 源字符串 中出现的次数,默认第一次。 【可选参数】
3、2个参数示例:
SELECT
INSTR('11223344','2') -- 返回:3
, INSTR('11223344','33') -- 返回:5
FROM DUAL ;
解析:字符串“2”在字符串“11223344”中,第一次出现的位置是:3。
注意:Oracle中字符串位置是从 1 开始,而不是0。
4、4个参数示例:
SELECT
INSTR('11223344','2',1,1) AS A -- 返回:3
, INSTR('11223344','2',1,2) AS B -- 返回:4
, INSTR('11223344','2',2,3) AS C -- 返回:0
, INSTR('11223344','2',-1,2) AS D -- 返回:3
, INSTR('11223344','2',-6,1) AS E -- 返回:3
FROM DUAL ;
A:从1开始,字符串“2”在字符串“11223344”中,出现的第 1次位置
B:从1开始,字符串“2”在字符串“11223344”中,出现的第 2次位置
C:从2开始,字符串“2”在字符串“11223344”中,出现的第 3次位置
D:从-1开始,字符串“2”在字符串“11223344”中,出现的第 2次位置
E:从-6开始,字符串“2”在字符串“11223344”中,出现的第 1次位置
可以通过图来理解,如下:
二、Oracle 模糊查询
1、直接Like
select * from tb where name like '%XX%';
2、用连接符 "||" --- MyBatis 使用
select * from tb where name like '%' || 'xx' ||'%';
3、用Concat函数 --- MyBatis 使用
select * from tb where name like concat('%',concat('xx','%'))
三、instr 函数 和 Oracle 模糊查询
1、Oracle Like :
select * from tb where name like '%XX%';
1.1 INSTR函数替换:
select * from tb where instr(name ,'xx') > 0
2、Oracle NOT Like
select * from tb where name not like '%XX%';
2.1 INSTR函数替换:
select * from tb where instr(name , 'xx') = 0
四、总结
1、instr 函数的基本用法,一般用2个参数就足够 , 常用于 Oracle中替换Like模糊查询,效率方面据说 instr函数由于 like ---- 没有验证!
2、为了阅读,sql都是全部小写的,实际开发工作中,建议大写,提升效率!
更多推荐