Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Split String and store into multiple columns

Author  Topic 

zain2
Starting Member

16 Posts

Posted - 2011-03-09 : 09:39:29
I got a table with 3 columns
ID,FNAME,TESTS
1, xyz, 'abc,tcdf,hjj,jj,jjj,j,j,j,j,j,j,'
2, zyx, 'bccc'ddd'ddd'ddd'ddd''ddd'
.. upto One thousand records

What I am trying to achieve is to separate Tests delimited string into number of columns and the output should be in this format: which would be stored in a separate table.


ID,Fname,Test1,Test2,Test3,Test4,Test5,Test6,Test7,Test8,Test9,Test10,Test11,Test12
1 xyz abc tcdf hjj jj jjj j j j j j j

First I tried to split the string (Test3) and stored in temporary table #tbl this works fine. The second step is to display each record in the above format...

I am not sure this is the best method....

I am really struggling and help in this regard is highly appreciated...

Here is split string code where i manage to split one record and not sure how to pull records from a existing table.

IF object_id('tempdb..#tbl') IS NOT NULL
BEGIN
DROP TABLE #tbl
END

create Table #tbl
(
Idx BIGINT IDENTITY (1,1),LineCounter INT,
SplitedLine varchar(8000),
Size INT
)
GO


DECLARE @WhileStart1 INT;
DECLARE @LoCofComma INT;
DECLARE @sep varchar(1);
Declare @str nvarchar(100);
DECLARE @LineCounter INT;
SET @sep =','
SET @str = '7,t,g,g,,df,d,d,d,'

SET @WhileStart1=0;
set @LineCounter = 1;
WHILE @WhileStart1 >-1
BEGIN;
SELECT @LoCofComma = CHARINDEX(@sep,@str,@WhileStart1);


IF @LoCofComma > 0

BEGIN;

INSERT INTO #tbl(SplitedLine,Size,LineCounter)
SELECT SUBSTRING(@str,@WhileStart1,@LoCofComma-@WhileStart1),@LoCofComma,@LineCounter
SET @WhileStart1 = @LoCofComma+1;
Set @LineCounter = @Linecounter+1

END;
ELSE
BEGIN;

INSERT INTO #tbl(SplitedLine,Size,LineCounter)
select '',@LoCofComma,@LineCounter
SET @WhileStart1 = -1;

END;
END;
RETURN;

go

select * from #tbl;

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2011-03-09 : 09:41:53
you can make use of the function here
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=76033

if the number of string is known you can use fnParseString() else you will need some dynamic SQL to do the job


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

zain2
Starting Member

16 Posts

Posted - 2011-03-09 : 09:44:51
Well number of columns are 12 but some columns can be empty...
Can anyone explain how the fnParseString FUNCTION please...
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2011-03-09 : 09:46:22
quote:
Originally posted by zain2

Well number of columns are 12 but some columns can be empty...


then use fnParseString()


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2011-03-09 : 10:06:24
copy & paste and execute

declare @str varchar(100)

select @str = 'abc,tcdf,hjj,jj,jjj,j,j,j,j,j,j'

select dbo.fnParseString(-1, ',', @str),
dbo.fnParseString(-2, ',', @str),
dbo.fnParseString(-3, ',', @str)



KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

zain2
Starting Member

16 Posts

Posted - 2011-03-09 : 10:07:35
When i tried to execute the function it shows an error

CREATE FUNCTION dbo.fnParseString
(
@Section SMALLINT,
@Delimiter CHAR,
@Text TEXT
)
RETURNS VARCHAR(8000)
AS

BEGIN
DECLARE @NextPos SMALLINT,
@LastPos SMALLINT,
@Found SMALLINT

IF @Section > 0
SELECT @Text = REVERSE(@Text)
SELECT @NextPos = CHARINDEX(@Delimiter, @Text, 1),
@LastPos = 0,
@Found = 1

WHILE @NextPos > 0 AND ABS(@Section) <> @Found
SELECT @LastPos = @NextPos,
@NextPos = CHARINDEX(@Delimiter, @Text, @NextPos + 1),
@Found = @Found + 1
RETURN CASE
WHEN @Found <> ABS(@Section) OR @Section = 0 THEN NULL

WHEN @Section > 0 THEN REVERSE(SUBSTRING(@Text, @LastPos + 1, CASE WHEN @NextPos = 0 THEN DATALENGTH(@Text) - @LastPos ELSE @NextPos - @LastPos - 1 END))

ELSE SUBSTRING(@Text, @LastPos + 1, CASE WHEN @NextPos = 0 THEN DATALENGTH(@Text) - @LastPos ELSE @NextPos - @LastPos - 1 END)
END

END



Msg 8116, Level 16, State 1, Procedure fnParseString, Line 16
Argument data type text is invalid for argument 1 of reverse function.
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2011-03-09 : 10:25:40
change @Text to varchar(max)

@Text TEXT varchar(max)



KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

zain2
Starting Member

16 Posts

Posted - 2011-03-09 : 10:59:21
It works great!! this is a life saving function...
I have to remove '"' from first and last column, this is how i use it..

dbo.fnParseString(-1, ',', Replace(Tests,'"','')) As Test1,
dbo.fnParseString(-2, ',', Tests) as Test2,
dbo.fnParseString(-3, ',', Replace(Tests,'"','')) As Test3

Can someone explain what is Tabled valued functions, Scalar Functions, Aggregate Function and System functions and what is the difference between them please..
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-03-09 : 11:02:40
see

http://www.sqlteam.com/article/user-defined-functions

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

ram235
Starting Member

2 Posts

Posted - 2011-11-01 : 11:55:47
if the number of strings is known then how can we do it dynamically???????????
Go to Top of Page

ram235
Starting Member

2 Posts

Posted - 2011-11-01 : 11:56:28
quote:
Originally posted by ram235

if the number of strings is unknown then how can we do it dynamically???????????

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-11-01 : 12:08:43
quote:
Originally posted by ram235

quote:
Originally posted by ram235

if the number of strings is unknown then how can we do it dynamically???????????




you can use below dynamic crosstab logic

http://sqlblogcasts.com/blogs/madhivanan/archive/2007/08/27/dynamic-crosstab-with-multiple-pivot-columns.aspx

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2011-11-03 : 08:54:56
or see if this also helps
http://beyondrelational.com/blogs/madhivanan/archive/2008/09/11/splitting-delimited-data-to-columns-set-based-approach.aspx

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

maxigodd
Starting Member

1 Post

Posted - 2012-11-07 : 05:11:22
quote:
Originally posted by khtan

copy & paste and execute

declare @str varchar(100)

select @str = 'abc,tcdf,hjj,jj,jjj,j,j,j,j,j,j'

select dbo.fnParseString(-1, ',', @str),
dbo.fnParseString(-2, ',', @str),
dbo.fnParseString(-3, ',', @str)



KH
[spoiler]Time is always against us[/spoiler]





Khtan I use your solution and it works great, but iwant to insert the result of the function, into anoher table and i am stack, can you please help me out.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-11-07 : 09:25:54
[code]
..
INSERT INTO YourTable
select dbo.fnParseString(-1, ',', @str),
dbo.fnParseString(-2, ',', @str),
dbo.fnParseString(-3, ',', @str)



[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -