Author |
Topic |
zain2
Starting Member
16 Posts |
Posted - 2011-03-09 : 09:39:29
|
I got a table with 3 columnsID,FNAME,TESTS1, xyz, 'abc,tcdf,hjj,jj,jjj,j,j,j,j,j,j,'2, zyx, 'bccc'ddd'ddd'ddd'ddd''ddd'.. upto One thousand recordsWhat 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,Test121 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 NULLBEGIN DROP TABLE #tblENDcreate Table #tbl (Idx BIGINT IDENTITY (1,1),LineCounter INT, SplitedLine varchar(8000), Size INT)GODECLARE @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 herehttp://www.sqlteam.com/forums/topic.asp?TOPIC_ID=76033if 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] |
|
|
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... |
|
|
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] |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2011-03-09 : 10:06:24
|
copy & paste and executedeclare @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] |
|
|
zain2
Starting Member
16 Posts |
Posted - 2011-03-09 : 10:07:35
|
When i tried to execute the function it shows an errorCREATE FUNCTION dbo.fnParseString( @Section SMALLINT, @Delimiter CHAR, @Text TEXT)RETURNS VARCHAR(8000)ASBEGINDECLARE @NextPos SMALLINT, @LastPos SMALLINT, @Found SMALLINTIF @Section > 0SELECT @Text = REVERSE(@Text)SELECT @NextPos = CHARINDEX(@Delimiter, @Text, 1),@LastPos = 0,@Found = 1WHILE @NextPos > 0 AND ABS(@Section) <> @FoundSELECT @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)ENDEND Msg 8116, Level 16, State 1, Procedure fnParseString, Line 16Argument data type text is invalid for argument 1 of reverse function. |
|
|
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] |
|
|
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 Test3Can someone explain what is Tabled valued functions, Scalar Functions, Aggregate Function and System functions and what is the difference between them please.. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
|
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??????????? |
|
|
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???????????
|
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
|
madhivanan
Premature Yak Congratulator
22864 Posts |
|
maxigodd
Starting Member
1 Post |
Posted - 2012-11-07 : 05:11:22
|
quote: Originally posted by khtan copy & paste and executedeclare @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. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-11-07 : 09:25:54
|
[code]..INSERT INTO YourTableselect dbo.fnParseString(-1, ',', @str), dbo.fnParseString(-2, ',', @str), dbo.fnParseString(-3, ',', @str)[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
|
|
|