将逗号分隔列值转换为行
我有一个表格示例与下面存储的数据
Id | String -------------- 1 abc,def,ghi 2 jkl,mno,pqr
我需要输出像..
Id | processedrows -------------- 1 abc 1 def 1 ghi 2 jkl 2 mno 2 pqr
我怎样才能做到SQL Server中的select查询?
尝试这个
SELECT A.[id], Split.a.value('.', 'VARCHAR(100)') AS String FROM (SELECT [id], CAST ('<M>' + REPLACE([string], ',', '</M><M>') + '</M>' AS XML) AS String FROM TableA) AS A CROSS APPLY String.nodes ('/M') AS Split(a);
请参阅这里
http://www.sqljason.com/2010/05/converting-single-comma-separated-row.html
试试这个。 你会得到你的输出。
SELECT id, PARSENAME(REPLACE(Split.a.value('.', 'VARCHAR(100)'),'-','.'),1) 'Values' FROM ( SELECT algorithms, CAST ('<M>' + REPLACE(string, ',', '</M><M>') + '</M>' AS XML) AS Data FROM <TableName> ) AS A CROSS APPLY Data.nodes ('/M') AS Split(a)
让我们试试下面的脚本:
declare @str varchar(max) SELECT @str = isnull(@str +',', '') + a.Value FROM (SELECT Value Entityvalue from Table) a select @str
SELECT EmployeeID, LTRIM(RTRIM(mnvalue('.[1]','varchar(8000)'))) AS Certs FROM ( SELECT EmployeeID,CAST('<XMLRoot><RowData>' + REPLACE(Certs,',','</RowData><RowData>') + '</RowData></XMLRoot>' AS XML) AS x FROM @t )t CROSS APPLY x.nodes('/XMLRoot/RowData')m(n)