sql like替代写法大全

本文是用oracle语法用作演示,使用到了instr,substr函数。

instr

like X%

select*from dual where 'asdfg' like 'a%';
select*from dual where instr('asdfg', 'a') = 1;

like %X

select*from dual where 'asdfg' like '%fg';
select*from dual 
 where instr('asdfg', 'fg') != 0 
   and instr('asdfg', 'fg') + length('fg') - 1 = length('asdfg');

like %X%

select*from dual where 'asdfg' like '%df%';
select*from dual where instr('asdfg', 'df') > 0;

substr

instr足以应对绝大多数场景,但是某个数据库偶尔会有无法使用instr的情况出现。因此把substr可以应对的两种方法列出来。

like X%

select*from dual where 'asd' like 'as%';
select*from dual where substr('asd', 1, length('as')) = 'as';

like %X

select*from dual where 'asd' like '%sd';
select*from dual where substr('asd', length('asd') - length('sd') + 1) = 'sd';

regexp_like

like可以算作regexp_like的子集,所有like语句都可以用regexp_like轻松改写。而regexp_like使用的是正则表达式,只要你会正则表达式,都可以直接使用regexp_like。

正则表达式的语法建议百度,这里只给出一个简单例子。

select*from dual where '123456' like '%3%5%';
select*from dual where regexp_like('123456', '*3*5*');
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值