How to split the data in a column with comma separation?
I am attaching the images for my Input and my expecting output.
The following is the input:
enter link description here
The following is my expected output:
enter link description here
Please help me how I will get my required output with a SQL Server query
Thanks in advance,
Phani Kumar.
declare #t Table (ID INT,Groupname VARCHAR(10),Pid VARCHAR(20))
insert into #t (ID,Groupname,Pid)values (1,'xxxx','123,568,562,25')
insert into #t (ID,Groupname,Pid)values (2,'yyyy','2,356,321')
insert into #t (ID,Groupname,Pid)values (3,'zzzz','7,898,569')
insert into #t (ID,Groupname,Pid)values (4,'sss','12345')
SELECT ID,Groupname,
PARSENAME(REPLACE(Split.a.value('.', 'VARCHAR(100)'),'-','.'),1) 'Values'
FROM
(
SELECT ID,Groupname,
CAST ('<M>' + REPLACE([Pid], ',', '</M><M>') + '</M>' AS XML) AS Data
FROM #t
) AS A
CROSS APPLY Data.nodes ('/M') AS Split(a)
Related
Let say I have a data
ID String
-------------------
1 John, Adam
Based on the below query transpose comma seperated data into rows
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);
Now, I would like to know what is the reason to have '.' and <M> in our query?
PN: Instead of flagging the post please let me know I will delete the post if it should not be post.
If you print out the string within the CAST, you will see that your text string has been turned into an XML string. The '.' in the split command is merely a location in which to start parsing the XML.
If it is sql server 2016 you can use string_split
create table commasep
(
id int identity(1,1)
,string nvarchar(100)
)
insert into commasep (string) values ('John, Adam'), ('test1,test2,test3')
select id, [value] as String from commasep
cross apply string_split(string,',')
WITH Numbers AS (SELECT Table.ProductNumber FROM Table WITH (NOLOCK))
returns ProductNumber like 1, 2, 3,4,5 - some are comma separated, so I want to split and than do proper SELECT on them with WHERE
What I got so far is:
SELECT #XML = CONVERT(xml,'<root><s>' + REPLACE(Numbers , ',' ,'</s><s>') + '</s></root>')
SELECT [ProductNumber ] = T.c.value('.','varchar(60)') FROM #XML.nodes('/root/s') T(c)
But I dont know how to convert selected SQL resource Numbers into string for XML conversion and not loose track of which ProductNumber were in which row
The problem in your query is you are assigning the converted xml to a variable, but here only the last row will be stored in that variable.
Try something like this.
SELECT Split.a.value('.', 'VARCHAR(100)') splt_num
FROM (SELECT Cast ('<M>'
+ Replace(ProductNumber, ',', '</M><M>')
+ '</M>' AS XML) AS Data
FROM yourtable) AS A
CROSS APPLY Data.nodes ('/M') AS Split(a)
How can I insert multiple xml files into a SQL Server table?
I have managed to get only 1 named file with this script:
CREATE TABLE #ZSK70 (Name varchar(25) , Value varchar(30),);
INSERT INTO #ZSK70 (Name, Value)
SELECT X.variable.query('Name').value('.', 'varchar(25)'),
X.variable.query('Value').value('.', 'varchar(30)')
FROM (
SELECT CAST(x AS XML)
FROM OPENROWSET(
BULK 'path+filename.xml',
SINGLE_BLOB) AS T(x)
) AS T(x)
CROSS APPLY x.nodes('CollectedData/variable') AS X(variable);
Any help is kindly appreciated.
i have table with one column having comma seperated values and I want in row..
like
col1
3,4,5
5,6,6
return result should be
col1
3
4
5
5
6
6
declare #tbl table(data nvarchar(100))
insert into #tbl values('1,2,3')
insert into #tbl values('4,5,6')
insert into #tbl values('7,8,9')
SELECT
Split.a.value('.', 'VARCHAR(100)') AS String
FROM (SELECT data,
CAST ('<M>' + REPLACE(data, ',', '</M><M>') + '</M>' AS XML) AS String
FROM #tbl) AS A CROSS APPLY String.nodes ('/M') AS Split(a);
I believe the below explains how to loop through comma separated values. You could just insert them into another variable to get your required output.
Splitting of comma separated values
You can use a recursive query to perform this split:
;with cte (item, list) as
(
select
cast(left(col1, charindex(',',col1+',')-1) as varchar(50)) item,
stuff(col1, 1, charindex(',',col1+','), '') list
from yourtable
union all
select
cast(left(list, charindex(',',list+',')-1) as varchar(50)) item,
stuff(list, 1, charindex(',',list+','), '') list
from cte
where list > ''
)
select item
from cte
See SQL Fiddle with Demo
Say I have this data:
site cell value
a b "1"
a c "2"
And I want the output for the format:
site value
a "b=1,c=2"
Is it possible with SQL?
PS: I am using access. But even if access does not support this particular syntax I would like to know any database that can.
Declare #tbl table ([site] nvarchar(100),Cell nvarchar(100),Value nvarchar(100))
INSERT INTO #tbl values('A','b','1')
INSERT INTO #tbl values('A','c','2')
SELECT [Site],
SUBSTRING(
(
select ' ,'+ Cell +'=' + CAST(value AS VARCHAR)
from #tbl b
WHERE a.[Site] = b.[Site]
FOR XML PATH('')
)
,3,100)
FROM #tbl a
GROUP BY a.[Site]
It is possible to do this in MySQL with GROUP_CONCAT