0

我有一个看起来有点像下面的字符串:

189 A 190 优点 191 68.6

现在我想要介于190和之间的值191- Merit

这可能吗?

4

1 回答 1

2

天真 - 你说你有一个字符串(即不是一列)。

declare @astring nvarchar(max);
set @astring = '189 A 190 Merit 191 68.6';

接下来的 2 个语句去掉了 190 和 191 之间的部分。

set @astring = stuff(@astring,1,patindex('%190%',@astring)+2,'');
set @astring = stuff(@astring,patindex('%191%',@astring+'191'),len(@astring),'');
set @astring = LTRIM(RTRIM(@astring));

select @astring;  -- 'Merit'

如果您的意思是表格列,那么

declare @t table (astring nvarchar(max));
insert @t select
'189 A 190 Merit 191 68.6' union all select
'189 A 19 Merit 191 68.6 oops bad string' union all select
'' union all select -- make sure it doesn't crash on empty string
null union all select -- ditto null
'189 C 190 Pass 191 50.1';

select astring, s2=stuff(s1,patindex('%191%',s1+'191'),len(s1),'')
from
(
select astring, s1=stuff(astring,1,patindex('%190%',astring+'190')+2,'')
from @t
) x

-- result
ASTRING                                      S2
189 A 190 Merit 191 68.6                     Merit
189 A 19 Merit 191 68.6 oops bad string      (null)
                                             (null)
(null)                                       (null)
189 C 190 Pass 191 50.1                      Pass
于 2012-10-16T09:54:52.783 回答