【问题标题】:Dynamically update column value with replacement for some pattern动态更新列值并替换某些模式
【发布时间】:2016-06-27 03:03:37
【问题描述】:

我的表格带有列 MailText,其值类似于

1. <strong>abc</strong>:<description1> <strong>bcd</strong>:<description2>
2. <strong>efg</strong>:<description3> <strong>hgl</strong>:<description7>

更新后我想要像

这样的值
1. <strong>abc</strong>:<abc> <strong>bcd</strong>:<bcd>
2. <strong>efg</strong>:<efg> <strong>hgl</strong>:<hgl>

请帮助动态替换它会将&lt;strong&gt;标签中的所有字符串更新为&lt;description&gt;&lt;strong&gt; 标签可以包含任何值。

【问题讨论】:

  • 一个好的开始:stackoverflow.com/help/how-to-ask
  • 将报价格式转换为代码格式。不确定数字 1.2. 是否是您记录的一部分。请在保存格式之前检查预览。请记住,编辑时在一行之前添加 4 个空格将文本格式化为代码。请不要缩进你的非代码内容。
  • 至于问题本身,您想要的结果也将hgl 切换为xyz。这是故意的吗?如果是这样,那里的逻辑是什么?
  • 你好 JNevill,这是错误的,hgl 应该是 hgl。我的意思是 hgl
  • 请做:使用预览窗口检查您的 HTML 是否出现在您的问题中(已编辑)。请不要:为您的问题添加紧急/优惠治疗请求(已删除)。

标签: sql regex replace sql-server-2012 sql-update


【解决方案1】:
CREATE FUNCTION GetString
(
    @s NVARCHAR(MAX)
)
RETURNS NVARCHAR(MAX)
AS
BEGIN
    DECLARE @trav                  NVARCHAR(2000) = @s,
            @length                INT,
            @count                 INT = 1,
            @startIndex            INT = 0,
            @endIndex              INT = 0,
            @replaceStartIndex     INT = 0,
            @repalceEndIndex       INT = 0,
            @replaceword           NVARCHAR(2000),
            @newWord               NVARCHAR(2000)

    SELECT @length = LEN(@Trav)
    WHILE ((@count + @startIndex) <= @length)
    BEGIN
        SET @startIndex = CHARINDEX('<strong>', @trav, @startIndex) + LEN('<strong>')
        IF (@startIndex > 8)
        BEGIN
            SET @endIndex = CHARINDEX('</strong>', @trav, @startIndex)
            SET @newWord = SUBSTRING(@trav, @startIndex, (@endIndex - @startIndex))
            SET @replaceStartIndex = CHARINDEX(':', @trav, @startIndex) + 2
            SET @repalceEndIndex = CHARINDEX('>', @trav, @replaceStartIndex)
            SET @replaceword = SUBSTRING(
                    @trav,
                    @replaceStartIndex,
                    (@repalceEndIndex - @replaceStartIndex)
                )
            --SELECT @replaceword as 'repword', @newWord as 'newword'
            SET @trav = REPLACE (@trav, @replaceword, @newWord)
            SET @count = @repalceEndIndex
        END
        ELSE
        BEGIN
            SET @count = @count + 1
        END
    END
    RETURN @trav
END
GO


IF OBJECT_ID('tempdb..#table') IS NOT NULL
    DROP TABLE #table

CREATE TABLE #table
(
    string VARCHAR(1000)
)
INSERT INTO #table
SELECT 
       '1. <strong>abc</strong>:<description1> <strong>bcd</strong>:<description2>'

INSERT INTO #table
SELECT 
       '2. <strong>efg</strong>:<description3> <strong>hgl</strong>:<description7>'


UPDATE #table
SET    string = [dbo].[GetString](#table.string)

SELECT *
FROM   #table

【讨论】:

    猜你喜欢
    • 2014-01-25
    • 2022-09-27
    • 2017-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-12
    相关资源
    最近更新 更多