求一高效率sql查找
求一高效sql查找?
表中一字符字段,数据量很大,
需要从里面找出以下资料,
字符中包含GD或DGH,
在GD或DGH后第9位必须为S或D
可以一个like解决吗?
AGD5-6888-6S-66 可以查出
BDGH5-6888-6D-66 可以查出
AGD5-688d8-6S-66 不可以查出
BDGH5-6888-6AD-66 不可以查出
------解决方案--------------------
------解决方案--------------------
select str1 from liketest where str1 like '%GD________[SD]%' or str1 like '%DGH________[SD]%'
------解决方案--------------------

------解决方案--------------------
表中一字符字段,数据量很大,
需要从里面找出以下资料,
字符中包含GD或DGH,
在GD或DGH后第9位必须为S或D
可以一个like解决吗?
AGD5-6888-6S-66 可以查出
BDGH5-6888-6D-66 可以查出
AGD5-688d8-6S-66 不可以查出
BDGH5-6888-6AD-66 不可以查出
------解决方案--------------------
create table jy
(x varchar(50))
insert into jy(x)
select 'AGD5-6888-6S-66' union all
select 'BDGH5-6888-6D-66' union all
select 'AGD5-688d8-6S-66' union all
select 'BDGH5-6888-6AD-66'
select x
from jy
where (charindex('GD',x,1)>0 or charindex('DGH',x,1)>0)
and substring(x,
case when charindex('GD',x,1)>0 then charindex('GD',x,1)+10
when charindex('DGH',x,1)>0 then charindex('DGH',x,1)+11 end
,1) in ('S','D')
/*
x
--------------------------------------------------
AGD5-6888-6S-66
BDGH5-6888-6D-66
(2 row(s) affected)
*/
------解决方案--------------------
select str1 from liketest where str1 like '%GD________[SD]%' or str1 like '%DGH________[SD]%'
------解决方案--------------------
------解决方案--------------------
If not object_id('[tb]') is null
Drop table [tb]
Go
Create table [tb]([str] nvarchar(17))
Insert [tb]
Select N'AGD5-6888-6S-66' union all
Select N'BDGH5-6888-6D-66' union all
Select N'AGD5-688d8-6S-66' union all
Select N'BDGH5-6888-6AD-66'
Go
--
Select * from [tb] where PATINDEX('%GD_________[SD]%',str)>0 or PATINDEX('%DGH_________[SD]%',str)>0
/*
str
-----------------
AGD5-688d8-6S-66
BDGH5-6888-6AD-66
(2 行受影响)
*/
select str from [tb] where str like '%GD________[SD]%' or str like '%DGH________[SD]%'