我正在使用以下内容将逗号分隔的字符串溢出到列中(SQL Server 2014):
function [dbo].[splitString](@input Varchar(max), @Splitter Varchar(99)) returns table as Return SELECT Split.a.value('.', 'NVARCHAR(max)') AS Data FROM ( SELECT CAST ('<M>' + REPLACE(@input, @Splitter, '</M><M>') + '</M>' AS XML) AS Data ) AS A CROSS APPLY Data.nodes ('/M') AS Split(a);
当我尝试拆分以下内容时:
Front Office,Food & Beverage,Housekeeping,Human Resource & Training,Reservation,Other
我收到以下错误:XML 解析:第 1 行,字符 82,非法名称字符
有没有办法在我的函数中包含特殊字符?
请尝试以下解决方案。
值得注意的点:
text()
.nodes()
TRY_CAST()
SQL
DECLARE @input Varchar(max) = 'Front Office,Food & Beverage,Housekeeping,Human Resource & Training,Reservation,Other' , @Splitter Varchar(99) = ','; SELECT Split.a.value('.', 'NVARCHAR(max)') AS Data FROM ( SELECT TRY_CAST('<M><![CDATA[' + REPLACE(@input, @Splitter, ']]></M><M><![CDATA[') + ']]></M>' AS XML) AS Data ) AS A CROSS APPLY Data.nodes ('/M/text()') AS Split(a);