DECLARE @rpmProvider varchar(200) = 'Prudhvi, raj,Lalith, Kumar';
DECLARE @xml xml
DECLARE @XMT_TEXT AS VARCHAR(200)
SET @XMT_TEXT = REPLACE(REPLACE(REPLACE(''''+ @rpmProvider + '''', ', ', '|'), ',', '</X><X>'), '|', ', ')
SET @xml = cast(('<X>' + @XMT_TEXT + '</X>') as xml)
SELECT N.value('.', 'varchar(50)') as value
FROM @xml.nodes('X') as T(N);
我的输出:
1. 'Prudhvi, raj
2. Lalith, Kumar'
预期输出:
1. 'Prudhvi, raj'
2. 'Lalith, Kumar'
答案 0 :(得分:1)
如果您删除所有与XML相关的代码,然后离开
DECLARE @rpmProvider varchar(200) = 'Prudhvi, raj,Lalith, Kumar';
select REPLACE(REPLACE(REPLACE(''''+ @rpmProvider + '''', ', ', '|'), ',', '</X><X>'), '|', ', ')
您会看到周围没有引号。结果是
'Prudhvi, raj</X><X>Lalith, Kumar'
您可以通过以下方式解决此问题:将替换字符串更改为''</X><X>''
,并在标签前后加上两个单引号:
select REPLACE(REPLACE(REPLACE(''''+ @rpmProvider + '''', ', ', '|'), ',', '''</X><X>'''), '|', ', ')
现在的结果是:
'Prudhvi, raj'</X><X>'Lalith, Kumar'
此代码非常非常脆弱。它取决于空格来分隔键,值和对。当涉及到字符串解析和操作时,SQL语言非常薄弱。这种解析应该在数据存储在数据库中之前进行。
答案 1 :(得分:0)
您可以尝试以下查询,
DECLARE @rpmProvider varchar(200) = 'Prudhvi, raj,Lalith, Kumar';
DECLARE @xml xml
DECLARE @XMT_TEXT AS VARCHAR(200)
SET @XMT_TEXT = REPLACE(REPLACE(REPLACE(''''+ @rpmProvider + '''', ', ', '|'), ',', '''</X><X>'''), '|', ', ')
SET @xml = cast(('<X>' + @XMT_TEXT + '</X>') as xml)
SELECT N.value('.', 'varchar(50)') as value
FROM @xml.nodes('X') as T(N);
答案 2 :(得分:0)
尝试
DECLARE @rpmProvider varchar(200) = 'Prudhvi, raj,Lalith, Kumar';
DECLARE @xml xml
DECLARE @XMT_TEXT AS VARCHAR(200)
SET @XMT_TEXT = REPLACE(REPLACE(REPLACE( @rpmProvider , ', ', '|'), ',', '</X><X>'), '|', ', ')
SET @xml = cast(('<X>' + @XMT_TEXT + '</X>') as xml)
SELECT ''''+N.value('.', 'varchar(50)')+'''' as value
FROM @xml.nodes('X') as T(N);
答案 3 :(得分:-1)
尝试如下
DECLARE @rpmProvider varchar(200) = 'Prudhvi, raj,Lalith, Kumar';
DECLARE @xml xml
DECLARE @XMT_TEXT AS VARCHAR(200)
SET @XMT_TEXT = REPLACE(REPLACE(REPLACE(''''+ @rpmProvider + '''', ', ', '|'), ',', '</X><X>'), '|', ', ')
SET @xml = cast(('<X>' + @XMT_TEXT + '</X>') as xml)
SELECT case when left( N.value('.', 'varchar(50)'),1)='''' then '' else
'''' end + N.value('.', 'varchar(50)') +
case when right( N.value('.', 'varchar(50)'),1)='''' then '' else
'''' end as value
FROM @xml.nodes('X') as T(N);
value
'Prudhvi, raj'
'Lalith, Kumar'