2014-10-08 43 views
1

我有一個表中的SQL Server:如何選擇行數據,在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 

現在我想用distinct這樣選擇從Supplimentary_keywords數據:

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) 

並且出現錯誤

消息9421,級別16,狀態1,行
XML分析:行1,字符11,非法命名字符

請幫助我如何做到這一點或糾正我,如果我做錯誤。

+0

替代爲了能夠糾正你,你就必須先**我們展示**您的代碼! – 2014-10-08 05:20:15

+0

請**不要**將代碼示例或示例數據放入註釋中 - 因爲您無法對其進行格式設置,所以**非常難**閱讀它....相反:**通過編輯將其更新爲**提供額外的信息!謝謝。 – 2014-10-08 05:25:23

+0

@marc_s對不起!我只是想向你展示我的代碼,這就是爲什麼 – Gitz 2014-10-08 05:28:28

回答

0

試試這個..

SELECT distinct Split.a.value('.', 'VARCHAR(100)') data 
      FROM (select id,Cast ('<M>' 
           + replace(Replace(suppl, ',', '</M><M>'),'&','&amp;') 
           + '</M>' AS XML) AS Data from #temp) AS A 
        CROSS APPLY Data.nodes ('/M') AS Split(a) 

如果您有任何其他的SPL相應字符替換它。

無效的特殊字符&它在XML

& - &amp; 
< - &lt; 
> - &gt; 
" - &quot; 
' - &#39; 
+0

yehhhh !!!現在它工作正常,並感謝您的幫助 – Gitz 2014-10-08 05:52:14

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) 
+0

你有Supporation_keywords列數據中的特殊字符 – 2014-10-08 05:34:00

+0

特殊字符表示??? – Gitz 2014-10-08 05:34:54

+0

無效特殊字符和其在XML &替代 - &安培 < - &LT > - &GT 「 - &QUOT ' - &#39 – 2014-10-08 05:36:56

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);