DECLARE @xml XMLSET @xml=CAST(REPLACE('',',',' '+'AA,AB,AC,AD'+' ') AS XML) SELECT T.C.value('.','varchar(20)') AS Result FROM @xml.nodes('/ROOT/X') T(C)
取前几条记录,如'10,102,10254,103265,541,2154,41,156'中第四个','前的字符串
DECLARE @sourceStr NVARCHAR(MAX)DECLARE @splitStr NVARCHAR(10)SET @sourceStr='10,102,10254,103265,541,2154,41,156'SET @splitStr=','DECLARE @xml XMLSET @xml=CAST(REPLACE('',@splitStr,' '+@sourceStr+' ') AS XML)SELECT TOP(3) Result+',' AS [text()]FROM ( SELECT T.C.value('.','varchar(20)') AS Result FROM @xml.nodes('/ROOT/X') T(C)) AS T1 FOR XML PATH('')