Not able to separate delimited data in sqlserver [duplicate] - sql

This question already has answers here:
T-SQL split string
(27 answers)
Closed 3 years ago.
I have column named Description, in that the rows are inserted along with a delimiter '-'.
I used the query to separate it. The query is mentioned below
select Description from Sheet1$
cross apply
SplitString('Description','-')
The columns have following data
Description
00000131-125
0000154-4625-4569-4568-45213

Try this below
DECLARE #Str AS TABLE ([Description] varchar(max) )
INSERT INTO #Str
SELECT '00000131-125' UNION ALL
SELECT '0000154-4625-4569-4568-45213'
SELECT Id,
LTRIM(RTRIM(Split.a.value('.','nvarchar(100)'))) AS [Description]
FROM
(
SELECT
ROW_NUMBER()OVER(ORDER BY (SELECT Null)) AS Id,
CAST('<S>'+(REPLACE([Description],'-','</S><S>')+'</S>') AS XML ) AS [Description]
FROM #Str
)AS A
CROSS APPLY [Description].nodes('S') AS Split(a)
Adding FOR XML PATH to the end of a query allows you to output the results of the query as XML elements, with the element name contained in the PATH argument.
Result
Id Description
---------------
1 00000131
1 125
2 0000154
2 4625
2 4569
2 4568
2 45213

Related

horizontal to vertical in one field data - SQL [duplicate]

This question already has answers here:
Delimited Function in SQL to Split Data between semi-colon
(2 answers)
Turning a Comma Separated string into individual rows
(16 answers)
Closed 5 years ago.
I have Input like this-
select ID,FIELD from TABLE
1| A,B,C,D
2|X,Y,Z
Output like this-
SELECT ID,FIELD from TABLE
1|A
1|B
1|C
1|D
2|X
2|Y
2|Z
Could someone please help me as how can I do it in SQL Server 2014 in an easy way ?
You can choose a string splitting function from Aaron Bertrand's Split strings the right way – or the next best way, and use it with cross apply to select the data from your table.
For this demonstration I've chosen to go with the XML string split function.
So first, create the function:
CREATE FUNCTION dbo.SplitStrings_XML
(
#List NVARCHAR(MAX),
#Delimiter NVARCHAR(255)
)
RETURNS TABLE
WITH SCHEMABINDING
AS
RETURN
(
SELECT Item = y.i.value('(./text())[1]', 'nvarchar(4000)')
FROM
(
SELECT x = CONVERT(XML, '<i>'
+ REPLACE(#List, #Delimiter, '</i><i>')
+ '</i>').query('.')
) AS a CROSS APPLY x.nodes('i') AS y(i)
);
GO
Then all you need to do is something like this:
SELECT ID, Item
FROM TABLE
CROSS APPLY
dbo.SplitStrings_XML(FIELD, ',')
See a live demo on rextester.
Also,you should probably read Is storing a delimited list in a database column really that bad?, where you will see a lot of reasons why the answer to this question is Absolutely yes!
You should XML With CROSS APPLY no need Explicit Function :
SELECT ID,
split.a.value('.', 'NVARCHAR(MAX)') [FIELD]
FROM
(
SELECT ID,
CAST('<M>'+REPLACE([Field], ',', '</M><M>')+'</M>' AS XML) AS String
FROM #TM
) AS a
CROSS APPLY String.nodes('/M') AS split(a);
Result :
ID FIELD
1 A
1 B
1 C
1 D
2 X
2 Y
2 Z

Pivot SQL Select Result to Desired Output

I am trying to convert a SQL result to desired output for analytic report.
Select statement and output is as follows:
SELECT QID, [Text], [Value] FROM TableA
OUTPUT SINGLE Question(QID)
OUTPUT MULTIPLE Question(QID)
Desired output result needed for report is as follows:
Can we achieve this result using Pivot table or similar way rather than using While and Cursor loop?
Thanks and regards,
Viju
The Above query will not work using PIVOT id qid values are same for all the records as we have to use any aggregate function in pivot
check the below query for reference
DECLARE #TAB TABLE (QID INT,[TEXT] VARCHAR(255),VALUE VARCHAR(255))
INSERT INTO #TAB VALUES (1,'WEAG','SDARG'),(1,'NUM','1'),(1,'ID','1')
,(2,'WEAG','RSTHGEST'),(2,'NUM','2'),(2,'ID','2')
,(3,'WEAG','SREVFGWR'),(3,'NUM','3'),(3,'ID','3')
SELECT * FROM #TAB
SELECT * FROM
(
SELECT T.QID,t.[TEXT],T.VALUE
FROM #TAB T
)A
PIVOT (MAX(VALUE) FOR [TEXT] IN (WEAG,ID) ) AS P
OUTPUT
QID WEAG ID
1 SDARG 1
2 RSTHGEST 2
3 SREVFGWR 3

Create function to split delimited seperated data for all records in the table [duplicate]

This question already has answers here:
Delimited Function in SQL to Split Data between semi-colon
(2 answers)
Turning a Comma Separated string into individual rows
(16 answers)
Closed 5 years ago.
I have Input like this-
select ID,FIELD from TABLE
1| A,B,C,D
2|X,Y,Z
Output like this-
SELECT ID,FIELD from TABLE
1|A
1|B
1|C
1|D
2|X
2|Y
2|Z
Could someone please help me as how can I do it in SQL Server 2014 in an easy way ?
You can choose a string splitting function from Aaron Bertrand's Split strings the right way – or the next best way, and use it with cross apply to select the data from your table.
For this demonstration I've chosen to go with the XML string split function.
So first, create the function:
CREATE FUNCTION dbo.SplitStrings_XML
(
#List NVARCHAR(MAX),
#Delimiter NVARCHAR(255)
)
RETURNS TABLE
WITH SCHEMABINDING
AS
RETURN
(
SELECT Item = y.i.value('(./text())[1]', 'nvarchar(4000)')
FROM
(
SELECT x = CONVERT(XML, '<i>'
+ REPLACE(#List, #Delimiter, '</i><i>')
+ '</i>').query('.')
) AS a CROSS APPLY x.nodes('i') AS y(i)
);
GO
Then all you need to do is something like this:
SELECT ID, Item
FROM TABLE
CROSS APPLY
dbo.SplitStrings_XML(FIELD, ',')
See a live demo on rextester.
Also,you should probably read Is storing a delimited list in a database column really that bad?, where you will see a lot of reasons why the answer to this question is Absolutely yes!
You should XML With CROSS APPLY no need Explicit Function :
SELECT ID,
split.a.value('.', 'NVARCHAR(MAX)') [FIELD]
FROM
(
SELECT ID,
CAST('<M>'+REPLACE([Field], ',', '</M><M>')+'</M>' AS XML) AS String
FROM #TM
) AS a
CROSS APPLY String.nodes('/M') AS split(a);
Result :
ID FIELD
1 A
1 B
1 C
1 D
2 X
2 Y
2 Z

How to split string in multiple column in sql server [duplicate]

This question already has answers here:
How to split a comma-separated value to columns
(38 answers)
Closed 7 years ago.
I have string "1:182:1,1:195:2,1:213:1".
I spllited the string with ',' in different rows. Now I want each rows single column to be splitted in 3 different columns in same row.
I tried using
SELECT LEFT(ThemeProperty, CHARINDEX(':', ThemeProperty) - 1) ,
RIGHT(ThemeProperty,
LEN(ThemeProperty) - CHARINDEX(':', ThemeProperty))
FROM #tempThemeProperty
But its output is
(No column name) (No column name)
1 182:1
1 195:2
1 213:1
But I want it to be
Column1 Column2 Column3
1 182 1
So any help would be appreciated.
DECLARE #Tmp TABLE (Id INT,Name VARCHAR(20))
INSERT #Tmp SELECT 1,'182:1'
INSERT #Tmp SELECT 2,'195:2'
INSERT #Tmp SELECT 3,'213:1'
--Using PARSENAME
SELECT Id,
PARSENAME(REPLACE(Name,':','.'),2) Value1,
PARSENAME(REPLACE(Name,':','.'),1) Value2
FROM #Tmp
This should be able to di it.. Let me know if it helps
This is one method but subject to sql injection
declare #s varchar(2000),#data varchar(2000)
select #s='1:182:1'
select #data=''''+replace(#s,':',''',''')+''''
exec('select '+#data)

Sql query to create single row to multiple row [duplicate]

This question already has answers here:
Turning a Comma Separated string into individual rows
(16 answers)
Closed 8 years ago.
I have a table as given below
ID Value
----- ----------
1 10,20,30
I need the result as
ID Value
--- -------
1 10
1 20
1 30
Thanks.
This will work
;with tmp(Id, value, Data) as (
select Id, LEFT(value, CHARINDEX(',',value+',')-1),
STUFF(value, 1, CHARINDEX(',',value+','), '')
from #Testdata
union all
select Id, LEFT(Data, CHARINDEX(',',Data+',')-1),
STUFF(Data, 1, CHARINDEX(',',Data+','), '')
from tmp
where Data > ''
)
select Id, value
from tmp
order by Id
Possible duplicate Turning a Comma Separated string into individual rows
Please try the following:
SELECT A.ID,
Split.a.value('.', 'VARCHAR(100)') AS Value
FROM (SELECT ID,
CAST ('<V>' + REPLACE(Value, ',', '</V><V>') + '</V>' AS XML) AS Value
FROM YourTable
) AS A
CROSS APPLY Value.nodes ('/V') AS Split(a);