declare @t table (col varchar(30)) insert into @t select 'qadabcd (1werwe)' union all select 'adfabcd'select left(col,charindex('abcd',col)-1) from @t /* qad adf */
declare @t table (col varchar(30)) insert into @t select 'qadabcd (1werwe)' union all select 'adfabcd (好1df)' union all select 'avabcd (wew3r231df)' union all select 'qaabcd (/1werwe)' union all select 'ssabcd (we好1df)' union all select 'sdfabcd (fwerwew3r231df)' union all select 'aa' --先判断是否存在abcd 免得没有abcd的时候报错 select case when charindex('abcd',col)>0 then left(col,charindex('abcd',col)-1) else col end as col from @t /* col ------------------------------ qad adf av qa ss sdf aa */
declare @t table (col varchar(30))
insert into @t
select 'qadabcd (1werwe)' union all
select 'adfabcd'select left(col,charindex('abcd',col)-1) from @t
/*
qad
adf
*/
declare @t table (col varchar(30))
insert into @t
select 'qadabcd (1werwe)' union all
select 'adfabcd (好1df)' union all
select 'avabcd (wew3r231df)' union all
select 'qaabcd (/1werwe)' union all
select 'ssabcd (we好1df)' union all
select 'sdfabcd (fwerwew3r231df)' union all
select 'aa'
--先判断是否存在abcd 免得没有abcd的时候报错
select
case when charindex('abcd',col)>0 then
left(col,charindex('abcd',col)-1) else col end as col
from @t
/*
col
------------------------------
qad
adf
av
qa
ss
sdf
aa
*/