I have a table Sample with data stored like below
Id | String |
---|---|
1 | abc,def,ghi |
2 | jkl,mno,pqr |
I need the output like..
Id | processedrows |
---|---|
1 | abc |
1 | def |
1 | ghi |
2 | jkl |
2 | mno |
2 | pqr |
How can I do the same with a select query in SQL Server?
I have a table Sample with data stored like below
Id | String |
---|---|
1 | abc,def,ghi |
2 | jkl,mno,pqr |
I need the output like..
Id | processedrows |
---|---|
1 | abc |
1 | def |
1 | ghi |
2 | jkl |
2 | mno |
2 | pqr |
How can I do the same with a select query in SQL Server?
try this
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);
refer here
http://www.sqljason.com/2010/05/converting-single-comma-separated-row.html
SELECT EmployeeID,
LTRIM(RTRIM(m.n.value('.[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)
EmployeeID
, and turning the string values into XML seems like massive overkill to me... –
Humanoid New way of doing this:
SELECT
a.Id,
b.value
FROM
Sample a
cross apply string_split(a.string,',') b
Let's try the below script:-
declare @str varchar(max)
SELECT @str = isnull(@str +',', '') + a.Value
FROM (SELECT Value Entityvalue from Table) a
select @str
Try with this. You will get your output.
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)
© 2022 - 2024 — McMap. All rights reserved.