May 3, 2015 at 4:19 pm
I have a table Sample with data stored like below
ID|STRING |
------------------------------------------------------------------
1| 'ENGLAN SPAIN' ITALY 'FRANCE GERMANY' BRAZIL
I need the output like..
-----------------
|ENGLAND SPAIN |
|---------------|
|ITALY |
|---------------|
|FRANCE GERMANY |
|---------------|
|BRAZIL|
-----------------
How can I do the same with a select query in SQL Server?
May 4, 2015 at 2:07 am
diogo.venturatomas (5/3/2015)
I have a table Sample with data stored like belowID|STRING |
------------------------------------------------------------------
1| 'ENGLAN SPAIN' ITALY 'FRANCE GERMANY' BRAZIL
I need the output like..
-----------------
|ENGLAND SPAIN |
|---------------|
|ITALY |
|---------------|
|FRANCE GERMANY |
|---------------|
|BRAZIL|
-----------------
How can I do the same with a select query in SQL Server?
Quick solution using the dbo.DelimitedSplit8K[/url] function
DECLARE @SAMPLE TABLE (ID INT NOT NULL, STRING VARCHAR(200) NOT NULL)
INSERT INTO @SAMPLE (ID,STRING) VALUES (1,'''ENGLAN SPAIN'' ITALY ''FRANCE GERMANY'' BRAZIL');
SELECT
S.ID
,LTRIM(X.Item) AS OUT_STRING
FROM @SAMPLE S
CROSS APPLY dbo.DelimitedSplit8K(S.STRING,CHAR(39)) AS X
WHERE LEN(X.Item) > 0;
Results
ID OUT_STRING
----------- -----------------
1 ENGLAN SPAIN
1 ITALY
1 FRANCE GERMANY
1 BRAZIL
May 4, 2015 at 5:45 pm
Hello Eirikur Eiriksson,
Your post is very good. But I have a question : I tried to apply the function to other by adding other words , but it has a problem, the fence does not separate words. I will show an example for you :
DECLARE @SAMPLE TABLE (ID INT NOT NULL, STRING VARCHAR(200) NOT NULL)
INSERT INTO @SAMPLE (ID,STRING)
VALUES (1,'''ENGLAN SPAIN'' ITALY ''FRANCE GERMANY'' BRAZIL ARGENTINA ''ROMENIA HOLLAND''');
--SELECT * FROM @SAMPLE
SELECT
S.ID
,LTRIM(X.Item) AS OUT_STRING
FROM @SAMPLE S
CROSS APPLY dbo.DelimitedSplit8K(S.STRING,CHAR(39)) AS X
WHERE LEN(X.Item) > 0;
RESULT:
IDOUT_STRING
1ENGLAN SPAIN
1ITALY
1FRANCE GERMANY
1BRAZIL ARGENTINA
1ROMENIA HOLLAND
BUT I WANT:
IDOUT_STRING
1ENGLAN SPAIN
1ITALY
1FRANCE GERMANY
1BRAZIL
1 ARGENTINA
1 ROMENIA HOLLAND
Thank You very mucg
May 4, 2015 at 9:27 pm
Perhaps if you use it twice with a little tweak. 😉
SELECT
S.ID
,LTRIM(ISNULL(Y.Item, X.Item)) AS OUT_STRING
FROM @SAMPLE S
CROSS APPLY dbo.DelimitedSplit8K(S.STRING,CHAR(39)) AS X
OUTER APPLY (SELECT s.Item
FROM (SELECT LTRIM(X.Item) Item) i
CROSS APPLY dbo.DelimitedSplit8K( i.Item, ' ') s
WHERE X.Item LIKE '% '
AND LEN(s.Item) > 0) Y
WHERE LEN(X.Item) > 0;
May 5, 2015 at 1:54 am
And a bit more robust version
DECLARE @SAMPLE TABLE (ID INT NOT NULL, STRING VARCHAR(200) NOT NULL)
INSERT INTO @SAMPLE (ID,STRING)
VALUES (1,'CANADA USA''ENGLAN SPAIN'' ITALY '' FRANCE GERMANY ''BRAZIL ARGENTINA''ROMENIA HOLLAND''');
SELECT
S.ID
,RTRIM(LTRIM(ISNULL(Y.Item, X.Item))) AS OUT_STRING
FROM @SAMPLE S
CROSS APPLY dbo.DelimitedSplit8K(S.STRING,CHAR(39)) AS X
OUTER APPLY (SELECT s.Item
FROM (SELECT RTRIM(LTRIM(X.Item)) Item) i
CROSS APPLY dbo.DelimitedSplit8K( i.Item, ' ') s
WHERE X.ItemNumber%2 = 1
AND LEN(s.Item) > 0) Y
WHERE LEN(X.Item) > 0;
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply