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