Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


spliting a varchar(max) into multiples


spliting a varchar(max) into multiples

Author
Message
Paarthasarathy
Paarthasarathy
SSC-Enthusiastic
SSC-Enthusiastic (153 reputation)SSC-Enthusiastic (153 reputation)SSC-Enthusiastic (153 reputation)SSC-Enthusiastic (153 reputation)SSC-Enthusiastic (153 reputation)SSC-Enthusiastic (153 reputation)SSC-Enthusiastic (153 reputation)SSC-Enthusiastic (153 reputation)

Group: General Forum Members
Points: 153 Visits: 90
Hi,
I have a need to split a varchar column into multiple columns based on a delimiter.

Hi have a variable named @a varchar(max)
it contains value similar to below
select @a = '1,a,b,20080101|2,c,d,20080102|3,e,f,20080102'
like i need to insert it into a table
with columns( a1 int, a2 char, a3 char, a4 datetime)

Is it worth writing a function with user defined table valued parameter or any other thoughts?

I would be happy to receive codes for accomplishing this?

Kindest Regards,
Paarthasarathy
Microsoft Certified Technology Specialist
http://paarthasarathyk.blogspot.com
ColdCoffee
ColdCoffee
SSCrazy
SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)

Group: General Forum Members
Points: 2319 Visits: 5545
Easily possible. Try "DelimitSplit8K Jeff Moden" in the site's search, you will find an excellent solution for your problem.
Eralper
Eralper
SSC-Enthusiastic
SSC-Enthusiastic (184 reputation)SSC-Enthusiastic (184 reputation)SSC-Enthusiastic (184 reputation)SSC-Enthusiastic (184 reputation)SSC-Enthusiastic (184 reputation)SSC-Enthusiastic (184 reputation)SSC-Enthusiastic (184 reputation)SSC-Enthusiastic (184 reputation)

Group: General Forum Members
Points: 184 Visits: 466
Hello,

Please try the following script

Within the script, you can find the source of the split function at Split String using XML - How to Convert or Split a Delimited String Values to Rows using T-SQL XML Commands

Besides split code, CROSS APPLY is also key here in this solution.
Also grouping and taking MAX is the trick.


create table splittedtable (
column1 int,
column2 varchar(5),
column3 varchar(5),
column4 date
)
GO;

declare @a varchar(max) = '1,a,b,20080101|2,c,d,20080102|3,e,f,20080102';

with cte as (
select
s.id,
column1 = case when t.id = 1 then t.val else null end,
column2 = case when t.id = 2 then t.val else null end,
column3 = case when t.id = 3 then t.val else null end,
column4 = case when t.id = 4 then t.val else null end
from dbo.SPLIT(@a,'|') s
cross apply dbo.SPLIT(s.val,',') t
)
insert into splittedtable
select
column1 = MAX(column1),
column2 = MAX(column2),
column3 = MAX(column3),
column4 = MAX(column4)
from cte
group by id

select * from splittedtable




Hoping to be helpful,

Eralper
SQL Server and T-SQL Tutorials and Articles
Microsoft Certification and Certification Exams
Paul White
Paul White
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10352 Visits: 11350
Eralper (7/20/2010)
...Split String using XML - How to Convert or Split a Delimited String Values to Rows using T-SQL XML Commands...

You have the right basic idea there, but using XML is not a good way to split strings in SQL Server.

There are some performance comparisons here

The script ColdCoffee mentioned earlier is efficient and appropriate for many situations.

For overall fastest performance, particularly on long strings and big sets of data, use a SQLCLR solution, such as this one.



Paul White
SQLPerformance.com
SQLblog.com
@SQL_Kiwi
Eralper
Eralper
SSC-Enthusiastic
SSC-Enthusiastic (184 reputation)SSC-Enthusiastic (184 reputation)SSC-Enthusiastic (184 reputation)SSC-Enthusiastic (184 reputation)SSC-Enthusiastic (184 reputation)SSC-Enthusiastic (184 reputation)SSC-Enthusiastic (184 reputation)SSC-Enthusiastic (184 reputation)

Group: General Forum Members
Points: 184 Visits: 466
Hi Paul,
I remember we have a discussion on splitting function, do you remember too Smile

In fact I could not measure how fast my split functions are.
I have also coded a recursive cte sql split function too. I guess it will not perform very good too. But I enjoy solving a problem using different methods.

By the way thanks for the link. I must say that I did not surprise to see the CLR is performing very well. Because splitting is not actually a database action. That is programming. And I do not expect sql engine to be very fast at row based actions, etc. But I expect it to be good at table level query commands.

Eralper
SQL Server and T-SQL Tutorials and Articles
Microsoft Certification and Certification Exams
Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)

Group: General Forum Members
Points: 45450 Visits: 39946
Paarthasarathy (7/20/2010)
Hi,
I have a need to split a varchar column into multiple columns based on a delimiter.

Hi have a variable named @a varchar(max)
it contains value similar to below
select @a = '1,a,b,20080101|2,c,d,20080102|3,e,f,20080102'
like i need to insert it into a table
with columns( a1 int, a2 char, a3 char, a4 datetime)

Is it worth writing a function with user defined table valued parameter or any other thoughts?

I would be happy to receive codes for accomplishing this?


Please see the following "how to" article for a T-SQL only solution...
http://www.sqlservercentral.com/articles/T-SQL/63003/

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Paul White
Paul White
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10352 Visits: 11350
Eralper (7/20/2010)
I remember we have a discussion on splitting function, do you remember too Smile
In fact I could not measure how fast my split functions are.

I can't say I do remember it specifically (sorry) but it is a question that comes up a lot, so I do lose track.
The performance differences may be minimal for a small number of rows (depending on the size of the rows, of course!) but the picture becomes very clear as we give the splitting more work to do. The XML method is the slowest of all the techniques I know, and it also bothers me because it is a misuse of the facility, and may fail for some strings due to illegal XML characters (as far as I remember, anyway).

I have also coded a recursive cte sql split function too. I guess it will not perform very good too. But I enjoy solving a problem using different methods.

Yes the recursive idea is nicer, at least from a theoretical point of view, but it does not perform well, as you say. This is mostly down to the SQL Server engine's implementation of recursion.

By the way thanks for the link. I must say that I did not surprise to see the CLR is performing very well. Because splitting is not actually a database action. That is programming. And I do not expect sql engine to be very fast at row based actions, etc. But I expect it to be good at table level query commands.

I agree with most of that. Set-based solutions will often out-perform even a good SQLCLR solution, but it's a question of picking the right tool for the job at hand.

Paul



Paul White
SQLPerformance.com
SQLblog.com
@SQL_Kiwi
Paul White
Paul White
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10352 Visits: 11350
Jeff Moden (7/20/2010)
Please see the following "how to" article for a T-SQL only solution...
http://www.sqlservercentral.com/articles/T-SQL/63003/

Yes, that's one of the top articles on the whole site. For some reason, I can never remember the title when I think about searching for it. That's my excuse for not posting it previously, and I am sticking to it :-)



Paul White
SQLPerformance.com
SQLblog.com
@SQL_Kiwi
Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)

Group: General Forum Members
Points: 45450 Visits: 39946
Paul White NZ (7/20/2010)
Jeff Moden (7/20/2010)
Please see the following "how to" article for a T-SQL only solution...
http://www.sqlservercentral.com/articles/T-SQL/63003/

Yes, that's one of the top articles on the whole site. For some reason, I can never remember the title when I think about searching for it. That's my excuse for not posting it previously, and I am sticking to it :-)


;-)

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Adam Haines
Adam Haines
SSCrazy
SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)SSCrazy (2.3K reputation)

Group: General Forum Members
Points: 2324 Visits: 3135
Since we have a known number of columns, we could technically split the data via a split function and then use PARSENAME to split the other data for us.

It makes the code clean and is pretty straight forward. Of course this only holds water, if there is a set number of columns and it is less than 4.


USE [tempdb]
GO

IF OBJECT_ID('dbo.fn_split') IS NOT NULL
DROP FUNCTION dbo.fn_split;
GO
CREATE FUNCTION dbo.fn_split(@arr AS NVARCHAR(MAX), @sep AS NCHAR(1))
RETURNS TABLE
AS
RETURN
WITH
L0 AS (SELECT 1 AS C UNION ALL SELECT 1) --2 rows
,L1 AS (SELECT 1 AS C FROM L0 AS A, L0 AS B) --4 rows (2x2)
,L2 AS (SELECT 1 AS C FROM L1 AS A, L1 AS B) --16 rows (4x4)
,L3 AS (SELECT 1 AS C FROM L2 AS A, L2 AS B) --256 rows (16x16)
,L4 AS (SELECT 1 AS C FROM L3 AS A, L3 AS B) --65536 rows (256x256)
,L5 AS (SELECT 1 AS C FROM L4 AS A, L4 AS B) --4,294,967,296 rows (65536x65536)
,Numbers AS (SELECT row_number() OVER (ORDER BY (SELECT 0)) AS N FROM L5)
SELECT
(n - 1) - LEN(REPLACE(LEFT(@arr, n-1), @sep, N'')) + 1 AS pos,
LTRIM(SUBSTRING(@arr, n, CHARINDEX(@sep, @arr + @sep, n) - n)) AS element
FROM Numbers
WHERE n <= LEN(@arr) + 1
AND SUBSTRING(@sep + @arr, n, 1) = @sep
AND Numbers.n <= 1000 -- make sure to change to an appropriate value
GO

DECLARE @var VARCHAR(MAX)
SET @var ='1,a,b,20080101|2,c,d,20080102|3,e,f,20080102'

SELECT
PARSENAME(REPLACE(element,',','.'),4) AS Col1,
PARSENAME(REPLACE(element,',','.'),3) AS Col2,
PARSENAME(REPLACE(element,',','.'),2) AS Col3,
PARSENAME(REPLACE(element,',','.'),1) AS Col4
FROM dbo.fn_split(@var,'|')





My blog: http://jahaines.blogspot.com
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search