在特定字符串后找到一个单词 - sql management studio 2012

时间:2013-07-17 09:41:46

标签: sql-server

我的文字列中包含以下字段(小样本,有很多变体):

INSERT INTO #retention1 VALUES ('hello Action=Refer non-action=non-refer')
INSERT INTO #retention1 VALUES ('bye Action=Follow non-action=non-refer')
INSERT INTO #retention1 VALUES ('hello non-action=non-refer Action=compare')

我需要在“Action =”之后找到这个词 示例答案: 第1条:参考 第2条:关注 第3项:比较

如果“Action =”之后的所有单词都是相同的长度,那么我就可以做到。不幸的是,所有变化都不知道长度。行动后的词几乎总是不同于上面的3个变种。

任何想法建议都将受到高度赞赏。

这是我用于“推荐”示例的代码,它只适用于:

BEGIN
DECLARE @P_string nvarchar (100),
@P_variable    nvarchar (100)/*,
@P_return_null nvarchar(100) = 'Y'*/

set @p_string = 'hello Action=Refer non-action=non-refer'
set @p_variable = 'Action'

select substring(@p_string, charindex(upper(@P_variable),upper(@P_string)) +len(@p_variable)+1,5) as trying


END;

3 个答案:

答案 0 :(得分:0)

您要查找的代码应首先查找字符串Action,然后查找该字后面的空格字符。之后,您就拥有了操纵源字符串所需的一切。

这应该有效:

DECLARE 
  @P_string nvarchar (100),
  @P_variable nvarchar (100),
  @idx1 int,
  @idx2 int

SET @p_string = 'hello Action=Refer non-action=non-refer'
SET @p_variable = 'Action'

SELECT
  @idx1 = charindex(lower(@P_variable),lower(@P_string)) + len(@p_variable) + 1,
  @idx2 = charindex(lower(' '), @P_string, @idx1) 

SELECT @idx1, @idx2

SELECT SUBSTRING(
  @p_string, 
  @idx1,
  @idx2 - @idx1) as trying

修改

在更全面地审查要求之后,我决定定制一个rCTE结构,我将其用于类似目的。在这里。

CREATE TABLE #retention1 (
  ID int,
  txt nvarchar (100)
)
INSERT INTO #retention1 VALUES (1, 'hello Action=Refer non-action=non-refer')
INSERT INTO #retention1 VALUES (2, 'bye Action=Follow non-action=non-refer')
INSERT INTO #retention1 VALUES (3, 'hello non-action=non-refer Action=compare')

;WITH T AS (
  SELECT
    ID,
    Row = 0,
    StartIdx = CAST(0 as int),
    EndIdx = CAST(0 as int),
    Result = CAST('' as nvarchar(max))
  FROM #retention1
  UNION ALL
  SELECT
    r1.ID,
    Row + 1,
    StartIdx = CAST(newstartidx AS int),
    EndIdx = CAST(EndIdx + newendidx as int),
    Result = CAST(newtoken as nvarchar(max))
  FROM 
    T
    JOIN #retention1 r1
      ON r1.ID = T.ID
    CROSS APPLY(
      SELECT newstartidx = EndIdx + 1
    ) calc1
    CROSS APPLY(
      SELECT newtxt = substring(r1.txt, newstartidx, len(r1.txt))
    ) calc2
    CROSS APPLY(
      SELECT patidx = charindex(' ', newtxt)
    ) calc3
    CROSS APPLY(
      SELECT newendidx = CASE 
        WHEN patidx = 0 THEN len(newtxt)
        ELSE patidx END
    ) calc4
    CROSS APPLY(
      SELECT newtoken = substring(r1.txt, newstartidx, newendidx)
    ) calc5
  WHERE newendidx > 0
) 
SELECT
  ID,
  --Result
  Name = left(Result, eqIdx - 1),
  Value = substring(Result, eqIdx + 1, len(Result) - eqIdx + 1)
FROM 
  T
  OUTER APPLY (
    SELECT eqIdx = charindex('=', Result)
  ) calc6
WHERE 
  Row != 0
  AND eqIdx != 0
ORDER BY ID

由于表中要解析多个表达式,因此在没有标识符的情况下引用它们时会遇到问题。所以,我在您的临时表中添加了ID。

CTE的输出还包含ID,您可以将其用作#retention1.ID。

的参考

答案 1 :(得分:0)

试试这个:

BEGIN

DECLARE @ret nvarchar (100),@P_string nvarchar (100),
@P_variable    nvarchar (100)/*,
@P_return_null nvarchar(100) = 'Y'*/

set @p_string = 'hello Action=Refer non-action=non-refer'
set @p_variable = 'Action'

select @ret=substring(@p_string, charindex(upper(@P_variable),upper(@P_string)) +len(@p_variable)+1,100) 

select substring(@ret,0,charindex(' ',lower(@ret),0))


END;

答案 2 :(得分:0)

字符串处理很多但有点作弊:在开头和结尾用@p_string扩展空格。

DECLARE @P_string nvarchar (100), @P_variable    nvarchar (100)

set @p_variable = ' Action='

set @p_string = ' hello Action=Refer non-action=non-refer '
select substring(substring(@p_string, charindex(upper(@P_variable),upper(@P_string))+1, len(@p_string)-len(@p_variable)),charindex('=',substring(@p_string, charindex(upper(@P_variable),upper(@P_string))+1, len(@p_string)-len(@p_variable)))+1,CHARINDEX(' ', substring(@p_string, charindex(upper(@P_variable),upper(@P_string))+1, len(@p_string)-len(@p_variable)))-LEN(@P_variable))

set @p_string = ' bye Action=Follow non-action=non-refer '
select substring(substring(@p_string, charindex(upper(@P_variable),upper(@P_string))+1, len(@p_string)-len(@p_variable)),charindex('=',substring(@p_string, charindex(upper(@P_variable),upper(@P_string))+1, len(@p_string)-len(@p_variable)))+1,CHARINDEX(' ', substring(@p_string, charindex(upper(@P_variable),upper(@P_string))+1, len(@p_string)-len(@p_variable)))-LEN(@P_variable))

set @p_string = ' hello non-action=non-refer Action=compare '
select substring(substring(@p_string, charindex(upper(@P_variable),upper(@P_string))+1, len(@p_string)-len(@p_variable)),charindex('=',substring(@p_string, charindex(upper(@P_variable),upper(@P_string))+1, len(@p_string)-len(@p_variable)))+1,CHARINDEX(' ', substring(@p_string, charindex(upper(@P_variable),upper(@P_string))+1, len(@p_string)-len(@p_variable)))-LEN(@P_variable))