SQL Server查询以替换额外的" ;;;;;人物



我的表中有以下行,我想删除重复的semicolons(;)。

ColumnName
Test;Test2;;;Test3;;;;Test4;
Test;;;Test2;Test3;;Test4;
Test;;;;;;;;;;;;;;;;;Test2;;;;Test3;;;;;Test4;

从上面的行中,我想删除重复的半隆(;),仅保留一个半隆(;)

喜欢以下

ColumnName
Test;Test2;Test3;Test4
Test;Test2;Test3;Test4
Test;Test2;Test3;Test4

谢谢Rajesh

我喜欢使用此模式。AFAIK,最初由Jeff Moden发布在SQLServerCentral(链接)上。我将终端半彩色留在所有行中,因为它们是单一发生的项目。 SUBSTRING()LEFT()删除。

CREATE TABLE #MyTable (MyColumn VARCHAR(500))
INSERT INTO #MyTable
SELECT 'Test;Test2;;;Test3;;;;Test4;'   UNION ALL
SELECT 'Test;;;Test2;Test3;;Test4;'     UNION ALL
SELECT 'Test;;;;;;;;;;;;;;;;;Test2;;;;Test3;;;;;Test4;' 
-- Where '^|' nor its revers '|^' is a sequence of characters that does not occur in the tablefield
SELECT REPLACE(REPLACE(REPLACE(MyColumn, ';', '^|'), '|^', ''), '^|', ';')
FROM #MyTable   
-- If you MUST remove terminal semi-colon
SELECT CleanText2   = LEFT(CleanText1, LEN(CleanText1)-1)
FROM
(
    SELECT CleanText1   = REPLACE(REPLACE(REPLACE(MyColumn, ';', '^|'), '|^', ''), '^|', ';')
    FROM #MyTable
)DT
DECLARE @TestTable TABLE(Column1  NVARCHAR(MAX)) 
INSERT INTO @TestTable VALUES 
('Test;Test2;;;Test3;;;;Test4;'),
('Test;;;Test2;Test3;;Test4;'),
('Test;;;;;;;;;;;;;;;;;Test2;;;;Test3;;;;;Test4;')
SELECT        replace(
                  replace(
                     replace(
                        LTrim(RTrim(Column1)), 
                     ';;',';|'),                    
                  '|;',''),                         
               '|','') AS Fixed
 FROM @TestTable

结果集

╔═════════════════════════╗
║          Fixed          ║
╠═════════════════════════╣
║ Test;Test2;Test3;Test4; ║
║ Test;Test2;Test3;Test4; ║
║ Test;Test2;Test3;Test4; ║
╚═════════════════════════╝
SELECT replace(replace(replace(Column1,';','<>'),'><',''),'<>',';') from table

尝试这个

它的afaik我刚刚取代了所有;by&lt;>,然后;