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.
Author |
Topic |
scelamko
Constraint Violating Yak Guru
309 Posts |
Posted - 2006-10-10 : 08:52:20
|
Guys,I have the data in the following format Name Key____________________ACS, AMS 2ABC 3XVY, XYZ 4Whenever I see the ',' delimiter in the row I should be able to create a seperate record with same keyName Key____________________ACS 2AMS 2ABC 3XVY 4XYZ 4Is there a way to do thisany suggestions/inputs would be helpfulThanks |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-10-10 : 08:56:43
|
There are function to be found here at SQLTeam for splitting CSV lists.Do a search.Then you have to add logic to insert every record but last, x times (where x is last record).Peter LarssonHelsingborg, Sweden |
 |
|
chiragkhabaria
Master Smack Fu Yak Hacker
1907 Posts |
Posted - 2006-10-10 : 09:06:18
|
or may be like this Declare @Table TAble( [Name] varchar(100), [Key] int)Insert @Table Select 'ACS,AMS',2 Union AllSelect 'ABC', 3 Union AllSelect 'XVY,XYZ', 4Select [Key],NullIf(SubString(',' + Name + ',' , Number , CharIndex(',' , ',' + Name + ',' , Number) - Number) , '') AS INROW FROM ( SELECT Top 100 percent a.i+b.i as Number FROM ( Select 0 i union all Select 1 union all Select 2 union all Select 3 union all Select 4 union all Select 5 union all Select 6 Union All Select 7 union all Select 8 union all Select 9 union all Select 10 union all Select 11 union all Select 12 Union All Select 13 union all Select 14 union all Select 15 ) as a, ( Select 0 i union all Select 16 union all Select 32 union all Select 48 union all Select 64 union all Select 80 union all Select 96 Union All Select 112 union all Select 128 union all Select 144 union all Select 160 union all Select 176 union all Select 192 Union All Select 208 union all Select 224 union all Select 240 ) as b order by 1 ) as F Inner Join @Tableon Number <= Len(',' + Name + ',') AND SubString(',' + Name + ',' , Number - 1, 1) = ',' AND CharIndex(',' , ',' + Name + ',' , Number) - Number > 0 Order by [key] --Output Key INROW ----------- ------2 ACS2 AMS3 ABC4 XVY4 XYZ Chiraghttp://chirikworld.blogspot.com/ |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-10-10 : 09:07:35
|
I used this split function CREATE FUNCTION dbo.fnSplitDelimitedString( @Text VARCHAR(8000), @Delimiter VARCHAR(8000))RETURNS @Parts TABLE ( i SMALLINT IDENTITY(0, 1) PRIMARY KEY CLUSTERED, Part VARCHAR(8000) )ASBEGIN DECLARE @LastIndex SMALLINT, @NextIndex SMALLINT IF @Text IS NULL OR DATALENGTH(@Text) = 0 RETURN IF @Delimiter IS NULL SELECT @Delimiter = ';' SELECT @LastIndex = 0, @NextIndex = 1 WHILE @NextIndex > 0 BEGIN SELECT @NextIndex = CHARINDEX(@Delimiter, @Text, @LastIndex + 1) INSERT @Parts ( Part ) SELECT CASE WHEN @NextIndex = 0 THEN SUBSTRING(@Text, @LastIndex + 1, DATALENGTH(@Text) - @LastIndex) ELSE SUBSTRING(@Text, @LastIndex + 1, @NextIndex - @LastIndex - 1) END SELECT @LastIndex = @NextIndex END RETURNEND This is the actual code to perform your task-- Prepare test dataDECLARE @Test TABLE ([Name] VARCHAR(1000), [Key] INT)INSERT @TestSELECT 'ACS, AMS', 2 UNION ALLSELECT 'ABC', 3 UNION ALLSELECT 'XVY, XYZ', 4-- Stage the dataDECLARE @CurrentName VARCHAR(1000), @Key INT, @MaxName VARCHAR(1000)SELECT @CurrentName = MIN([Name]), @MaxName = MAX([Name])FROM @TestDECLARE @Stage TABLE ([Name] VARCHAR(1000), [Key] INT)WHILE @CurrentName <= @MaxName BEGIN SELECT @Key = [Key] FROM @Test WHERE [Name] = @CurrentName INSERT @Stage SELECT LTRIM(RTRIM(Part)), @Key FROM dbo.fnSplitDelimitedString(@CurrentName, ',') SELECT @CurrentName = MIN([Name]) FROM @Test WHERE [Name] > @CurrentName END-- Show the outputSELECT * FROM @Stage Peter LarssonHelsingborg, Sweden |
 |
|
chiragkhabaria
Master Smack Fu Yak Hacker
1907 Posts |
Posted - 2006-10-10 : 09:12:07
|
Peter, does performance affects between while loop and cross join ?Chiraghttp://chirikworld.blogspot.com/ |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-10-10 : 09:16:45
|
The cartesian product CROSS JOIN is costsome. It also calculates all possible combinations, in your case 256 of them for all possible string lengths.If there are rows with larger strings (as VARCHAR 8000), I think it would take longer time than using a function like the one above, because then you have to always calculate 8000 combinations even if some strings are much shorter.It looks like a real test is on?Peter LarssonHelsingborg, Sweden |
 |
|
madhivanan
Premature Yak Congratulator
22864 Posts |
|
|
|
|
|
|