我在SQL Server中有一个表:
T_Id Supplimentary_keywords
-------------------------------------------------------------------------------------------
1 Animal, Animals, 1, One, Single,live,living Organism
2 Animals, Animal, Two, 2,live,living Organism
3 Animals, Animal, Three, 3,live,living Organism
4 Animals, Animal, Four, 4,live,living Organism
5 Animals, Animal, 5, Five,live,living Organism
6 Group Of Animals, Small Group, Group, Groups, Small Size Group
7 Group Of Animals, Animals, Animal, Group, Groups, Grouping,live,living Organism
8 Group Of Animals, Animals, Animal, Group, Groups, Grouping,large Group,live
9 Head, Heads
10 Neck, Necks
现在我想从Supplimentary_keywords
选择distinct
这样的数据:
Supplimentary_keywords
----------------------
Animal
Animals
1
One
Single
live
living Organism
Two
2
live
Three
3
.......
我正在使用以下代码
SELECT DISTINCT
Split.a.value('.', 'VARCHAR(100)') Kwd
FROM
(SELECT
T_Id,
CAST('<M>' + Replace(Supplimentary_keywords, ',', '</M><M>')
+ '</M>' AS XML) AS Data from KWD_Theaurus_tbl) AS A
CROSS APPLY
data.nodes ('/M') AS Split(a)
并收到错误
Msg 9421,Level 16,State 1,Line 1
XML解析:第1行,第11个字符,非法名称字符
如果我做错了,请帮助我如何做到这一点或纠正我。
答案 0 :(得分:1)
create table #temp (id int,suppl varchar(1000))
insert into #temp
select 1 , 'Animal, Animals, 1, One, Single,live,living Organism'
union all
select 2 , 'Animals, Animal, Two, 2,live,living Organism'
union all
select 3 , 'Animals, Animal, Three, 3,live,living Organism'
union all
select 4 , 'Animals, Animal, Four, 4,live,living Organism'
union all
select 5 , 'Animals, Animal, 5, Five,live,living Organism'
union all
select 6 , 'Group Of Animals, Small Group, Group, Groups, Small Size Group'
union all
select 7 , 'Group Of Animals, Animals, Animal, Group, Groups, Grouping,live,living Organism'
union all
select 8 , 'Group Of Animals, Animals, Animal, Group, Groups, Grouping,large Group,live'
union all
select 9 , 'Head, Heads'
union all
select 10 , 'Neck, Necks'
SELECT distinct Split.a.value('.', 'VARCHAR(100)') data
FROM (select id,Cast ('<M>'
+ Replace(suppl, ',', '</M><M>')
+ '</M>' AS XML) AS Data from #temp) AS A
CROSS APPLY Data.nodes ('/M') AS Split(a)
答案 1 :(得分:0)
试试这个..
SELECT distinct Split.a.value('.', 'VARCHAR(100)') data
FROM (select id,Cast ('<M>'
+ replace(Replace(suppl, ',', '</M><M>'),'&','&')
+ '</M>' AS XML) AS Data from #temp) AS A
CROSS APPLY Data.nodes ('/M') AS Split(a)
如果您有任何其他spl字符,请相应地替换它。
无效的特殊字符&amp;它在xml中的替代品
& - &
< - <
> - >
" - "
' - '
答案 2 :(得分:0)
SELECT DISTINCT Split.a.value('.', 'VARCHAR(100)') AS Supplimentary_keywords
FROM (SELECT id,
Cast ('<M>'
+ Replace(Supplimentary_keywords, ',', '</M><M>')
+ '</M>' AS XML) AS Supplimentary_keywords
FROM #temp) AS A
CROSS APPLY Supplimentary_keywords.nodes ('/M') AS Split(a);