sql拆分并替换表中的字符串

时间:2017-03-30 01:58:05

标签: sql-server tsql

我在表格中有数据如下,并试图使用替换来做,但我最终得到双分号';;;使用替换后结束时使用分号或分号。我可以使用拆分和替换来实现这个或任何其他方式??

    Test attempts;My attempts using operation;Additions, deletions, and modifications 
    Test attempts;My attempts using operation
    Test attempts;access to information;attempts using nex;Additions, deletions, and modifications
    Test attempts;Changes to programs

用emptystring AND替换'我的尝试使用操作'     将“尝试使用nex”替换为“在上面的数据列中尝试使用imex”

    Result should be:

    Test attempts;Additions, deletions, and modifications 
    Test attempts-- No semicolon should be there if it ends up at end with that after replace
    Test attempts;access to information;attempts using imex;Additions, deletions, and modifications

Test attempts;Changes to programs

3 个答案:

答案 0 :(得分:1)

尽可能简单

declare @test table (content varchar(8000))
insert into @test values ('Test attempts;My attempts using operation;Additions, deletions, and modifications ')
insert into @test values ('Test attempts;My attempts using operation')
insert into @test values ('Test attempts;access to information;attempts using nex;Additions, deletions, and modifications')
insert into @test values ('Test attempts;Changes to programs')

select 
    case charindex(';My attempts using operation',content) when 0 then content 
        else (substring(content,0,charindex(';My attempts using operation',content))
                +substring(content,charindex(';My attempts using operation',content)+len(';My attempts using operation'),len(content)))
        end
        from @test

答案 1 :(得分:0)

首先是样本数据

IF OBJECT_ID('tempdb..#t') IS NOT NULL DROP TABLE #t;
CREATE TABLE #t(OriginalString varchar(1000));
INSERT #t
VALUES 
('Test attempts;My attempts using operation;Additions, deletions, and modifications'),
('Test attempts;My attempts using operation'),
('Test attempts;access to information;attempts using nex;Additions, deletions, and modifications'),
('Test attempts;Changes to programs');

这个怎么样:

WITH prep(NewString) AS
(
  SELECT
    REPLACE(REPLACE(REPLACE(
      OriginalString, 'attempts using nex', 'attempts using imex'
    ), 'My attempts using operation', ''), ';;', ';')
  FROM #t
)
SELECT
  NewString = 
    CASE 
      WHEN NewString LIKE '%;' 
      THEN SUBSTRING(NewString, 1, LEN(NewString)-1) 
      ELSE NewString 
    END
FROM prep;

答案 2 :(得分:0)

希望这会有所帮助。根据您的请求,字符串将在;上拆分并重新构建,不包含排除的字符串

DECLARE @RemoveString VARCHAR(8000) = 'My attempts using operation'

;WITH cte_TestData (StringValue)
AS (
    SELECT 'Test attempts;My attempts using operation;Additions, deletions, and modifications '
    UNION ALL
    SELECT 'Test attempts;My attempts using operation'
    UNION ALL
    SELECT 'Test attempts;access to information;attempts using nex;Additions, deletions, and modifications'
    UNION ALL
    SELECT 'Test attempts;Changes to programs'
    UNION ALL
    SELECT 'Test attempts'
    ),
cte_Sequencer
AS (
    SELECT ROW_NUMBER() OVER (
            ORDER BY (
                    SELECT NULL
                    )
            ) AS Id,
        StringValue
    FROM cte_TestData
    ),
cte_XMLBuilder
AS (
    SELECT Id,
        StringValue,
        CAST('<M>' + REPLACE(StringValue, ';', '</M><M>') + '</M>' AS XML) AS XMLData
    FROM cte_Sequencer
    ),
StringSplitter
AS (
    SELECT C1.Id,
        ROW_NUMBER() OVER (
            PARTITION BY C1.Id ORDER BY (
                    SELECT NULL
                    )
            ) AS SeqId,
        C2.StringValue,
        SPLITS.ABC.value('.', 'varchar(MAX)') AS Data
    FROM cte_XMLBuilder C2
    INNER JOIN cte_XMLBuilder C1
        ON C2.StringValue = C1.StringValue
    CROSS APPLY C1.XMLData.nodes('/M') AS SPLITS(ABC)
    )
SELECT DISTINCT ID,
    StringValue,
    STUFF((
            SELECT ';' + b.Data AS [text()]
            FROM StringSplitter b
            WHERE b.Id = a.Id
                AND Data != @RemoveString
            ORDER BY id,
                SeqId
            FOR XML PATH('')
            ), 1, 1, '') AS EditedString
FROM StringSplitter a