Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SQL Server查询以替换额外的“;”&引用;人物_Sql_Sql Server - Fatal编程技术网

SQL Server查询以替换额外的“;”&引用;人物

SQL Server查询以替换额外的“;”&引用;人物,sql,sql-server,Sql,Sql Server,我的表中有以下行,我想删除重复的分号(;) 从上面的行中,我想删除重复的分号(;),只保留一个分号(;) 如下 ColumnName Test;Test2;Test3;Test4 Test;Test2;Test3;Test4 Test;Test2;Test3;Test4 谢谢 拉杰什我喜欢使用这种模式。AFAIK,最初由Jeff Moden在SQLServerCentral()上发布。 我在所有行中都留下了分号,因为它们是单发生项子字符串()或左() CREATE TABLE #MyTable

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

从上面的行中,我想删除重复的分号(;),只保留一个分号(;)

如下

ColumnName
Test;Test2;Test3;Test4
Test;Test2;Test3;Test4
Test;Test2;Test3;Test4
谢谢
拉杰什

我喜欢使用这种模式。AFAIK,最初由Jeff Moden在SQLServerCentral()上发布。 我在所有行中都留下了分号,因为它们是单发生项<代码>子字符串()或
左()

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 table\field
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
结果集

╔═════════════════════════╗
║          Fixed          ║
╠═════════════════════════╣
║ Test;Test2;Test3;Test4; ║
║ Test;Test2;Test3;Test4; ║
║ Test;Test2;Test3;Test4; ║
╚═════════════════════════╝

选择replace(replace(replace)(第1列,;,,,,,,)”>到目前为止您尝试了什么?你好,马克,谢谢您的快速解决方案。您能告诉我如何从行中删除最后一个半圆吗?
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