SSMS 2008 / SQL - 如何提取特定的文本字符串

时间:2016-11-29 11:01:16

标签: sql sql-server tsql

挣扎着这个。

我有一个名为requeststext的表,这里有两列感兴趣;

  • 'Title'(nvarchar(255),not null)
  • 'Description'(ntext,null)

我想更新多行,将每一行的“标题”设置为“描述”字段中的特定文字。

示例;

总是

标题;

New incident has been raised

描述是一个非常大的数据字符串,它还包含换行符,示例提取将是;

它如何出现在SSMS *中;

This Request has been raised:<CRLF>==================================================[CRLF]Request 123456: My PC doesn't work - please help![CRLF]==================================================[CRLF]Type: Incident[CRLF]...

它在应用程序中的显示方式(更易于阅读);

This Request has been raised: 
==================================================
Request 123456: My PC doesn't work - please help!
==================================================
Type: Incident
...

关于描述的注意事项;

  • *我添加了[CRLF],因为这些没有出现在SSMS中
  • 字段中的文字/字符保持在相同的位置,唯一不同的部分是“我的电脑无法正常工作 - 请帮助!”部分,这可能会有不同的文字和长度。

所需的结果将标题更改为:

"My PC doesn't work - please help!"

我一直试图让PATINDEX与SUBSTRING合作,但我似乎无法让它工作。

3 个答案:

答案 0 :(得分:1)

尝试这样

DECLARE @tbl TABLE ([Description] VARCHAR(MAX));
INSERT INTO @tbl VALUES 
(
'This Request has been raised:
==================================================
Request 123456: My PC doesn''t work - please help!
==================================================
Type: Incident'
);

SELECT SUBSTRING(NoLineBreaks.s,posFirst,CHARINDEX('=',NoLineBreaks.s,posFirst)-posFirst)
FROM @tbl AS tbl
CROSS APPLY(SELECT REPLACE(REPLACE([Description],CHAR(13),''),CHAR(10),'')) AS NoLineBreaks(s)
CROSS APPLY(SELECT CHARINDEX(': ',NoLineBreaks.s)+2) AS FirstColon(posFirst);

第一个CROSS APPLY抛弃CHAR(13)CHAR(10)以摆脱换行符。第二个查找第一个冒号,后面跟着空白。

最后,您使用SUBSTRING并从找到的位置开始,直到第一个=

更新:将此用于表格的更新

您使用一些细节编辑了您的问题(请避免变色龙问题!)。问题是使用多列的提取文本更新标题。

为此,我建议使用可更新的CTE

注意声明的表变量@tbl只是一个模型来创建测试场景。 您不需要这个!! 只需将CTE中的@tbl替换为您实际的表格名称即可。 关注真实数据!!

DECLARE @tbl TABLE (Title VARCHAR(100),[Description] VARCHAR(MAX));
INSERT INTO @tbl VALUES 
('New incident has been raised'
,'This Request has been raised:
==================================================
Request 123456: My PC doesn''t work - please help!
==================================================
Type: Incident'
)
,('New incident has been raised'
,'This Request has been raised:
==================================================
Request 123456: Some other text
==================================================
Type: Incident'
);

--This is the table's content before the update
SELECT * FROM @tbl;

- CTE返回两列:一列是标题,另一列是新内容

WITH UpdateableCTE AS
(
    SELECT SUBSTRING(NoLineBreaks.s,posFirst,CHARINDEX('=',NoLineBreaks.s,posFirst)-posFirst) AS NewTitle
          ,tbl.Title
    FROM @tbl AS tbl
    CROSS APPLY(SELECT REPLACE(REPLACE([Description],CHAR(13),''),CHAR(10),'')) AS NoLineBreaks(s)
    CROSS APPLY(SELECT CHARINDEX(': ',NoLineBreaks.s)+2) AS FirstColon(posFirst)
)
UPDATE UpdateableCTE SET Title=NewTitle;

--The new content after the update
SELECT * FROM @tbl;

答案 1 :(得分:0)

您需要一个拆分功能,以便您可以通过分隔符对数据进行切片,然后获得所需的输出,这是一个很好的例子How to split string using delimiter char using T-SQL?

答案 2 :(得分:0)

另一种方式:

-- your sample data
DECLARE @tbl TABLE (Title VARCHAR(100),[Description] VARCHAR(MAX));
INSERT INTO @tbl VALUES 
('New incident has been raised'
,'This Request has been raised:
==================================================
Request 123456: My PC doesn''t work - please help!
==================================================
Type: Incident'
)
,('New incident has been raised'
,'This Request has been raised:
==================================================
Request 123456: Some other text
==================================================
Type: Incident'
);

-- alternative solution:
WITH 
s1 AS 
(
  SELECT title, string = SUBSTRING([Description], CHARINDEX('=', [Description])+50, 1000)
  FROM @tbl
),
s2 AS 
(
  SELECT title, string, start = CHARINDEX(':', string)
  FROM s1
)
SELECT 
  Title, 
  [Description] = SUBSTRING(string, start+1, CHARINDEX('=', string, start)-(start+1))
FROM s2;