sql在字符串上查找字符

时间:2017-03-24 09:15:06

标签: sql substring charindex

我有字符串如图所示,我需要获取“INTERNAL_INSTRUCTION_NUM”旁边的值意味着我需要获取值“9076”意味着在我的表INTERNAL_INSTURCTION_NUM中具有值9076.我需要从字符串中检索。放入星号是我需要的字符串。我需要将参数作为INTERNAL_INSTURCTION_NUM传递,并将值返回为“9076”

下面链接中附加的字符串。 https://drive.google.com/open?id=0BypBdg0gwa3Bb3FlakFVY29QTGc

1 个答案:

答案 0 :(得分:0)

这是 SQL Server 版本:

declare @param nvarchar(max) = N'INTERNAL_INSTRUCTION_NUM';

declare 
    @pos int,
    @temp nvarchar(max);

set @pos = charindex(@param, @test);

set @temp = substring(@test, @pos + len(@param), len(@test) 
                 - @pos - len(@param));

-- find first # after param
set @pos = charindex('#', @temp);
set @temp = substring(@temp, @pos+1, len(@temp) - @pos);

-- find second # after param
set @pos = charindex('#', @temp);
set @temp = substring(@temp, @pos+1, len(@temp) - @pos);

-- find the value we are looking for assuming it is enclosed between ",+" and "?%"
select substring(@temp, 3, len(@temp) - 4)