Parse specific strings in SQL Server [duplicate] - sql

This question already has answers here:
How do I split a delimited string so I can access individual items?
(46 answers)
Closed 7 years ago.
I' ve got this string:
DealerCode = [MAZ3].AccountID:[4340].StartDate=[2015-06-01]
select parsename('DealerCode = [MAZ3].AccountID:[4340].StartDate=[2015-06-01]', 1)
and so on gives me null. How should i change my query to get values between dots?

Try this :
DECLARE #param NVARCHAR(MAX)
SET #param = 'DealerCode = [MAZ3].AccountID:[4340].StartDate=[2015-06-01]'
SELECT
Split.a.value('.', 'VARCHAR(100)') AS CVS
FROM
(
SELECT CAST ('<M>' + REPLACE(#param, '.', '</M><M>') + '</M>' AS XML) AS CVS
) AS A CROSS APPLY CVS.nodes ('/M') AS Split(a)

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

Transpose comma seperated data into rows - some clarification

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,',')

SQL character delimited rows [duplicate]

This question already has answers here:
SQL query to split column data into rows
(3 answers)
Closed 7 years ago.
I have a SQL column with the following values:
Col1
22;34;56;70
I need to be able to create a query that will return 4 rows from that i.e:
Col1
22
34
56
70
How would I split by ;?
Try this:
create table #t(id varchar(max))
insert into #t values('22;34;56;70')
SELECT
Split.a.value('.', 'VARCHAR(100)') AS String
FROM (SELECT [id],
CAST ('<M>' + REPLACE([id], ';', '</M><M>') + '</M>' AS XML) AS String
FROM #t) AS A CROSS APPLY String.nodes ('/M') AS Split(a);

Split values into separate rows

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)

Select part of VARCHAR value separated by the same special characters [duplicate]

This question already has answers here:
How do I split a delimited string so I can access individual items?
(46 answers)
Closed 8 years ago.
I am using SQL Server 2008 R2
This is the value that I have :
DECLARE #DBB varchar(200) = 'A2gg3h.B2g3ghh3.Cggh3663.D1jhg23.Eh2hjj2g'
Returning the 2 outer values are easy enough :
SELECT LEFT(#DBB, CHARINDEX('.', #DBB)-1)
SELECT RIGHT(#DBB, CHARINDEX('.', #DBB)-1)
How would I alter script in order to select values :
1. 'Bg2g3ghh3'
2. 'Chggh3663'
3. 'Dh1jhg23'
Using CHARINDEX would only bring back (LEFT) 7 and (RIGHT) 9.
Thanks
Use this.
DECLARE #param NVARCHAR(MAX)
SET #param = 'A2gg3h.B2g3ghh3.Cggh3663.D1jhg23.Eh2hjj2g'
SELECT
Split.a.value('.', 'VARCHAR(100)') AS CVS
FROM
(
SELECT CAST ('<M>' + REPLACE(#param, '.', '</M><M>') + '</M>' AS XML) AS CVS
) AS A CROSS APPLY CVS.nodes ('/M') AS Split(a)
TRY THIS:
DECLARE #string VARCHAR(MAX),
#Split CHAR(1),
#X xml
SELECT #string = 'A2gg3h.B2g3ghh3.Cggh3663.D1jhg23.Eh2hjj2g',
#Split = '.'
SELECT #X = CONVERT(xml,'<root><s>' + REPLACE(#string,#Split,'</s><s>') + '</s></root>')
SELECT T.c.value('.','varchar(max)') AS Result
FROM #X.nodes('/root/s') T(c)