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 2000 Forums
 Transact-SQL (2000)
 Data Manipulation

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 2
ABC 3
XVY, XYZ 4

Whenever I see the ',' delimiter in the row I should be able to create a seperate record with same key

Name Key
____________________
ACS 2
AMS 2
ABC 3
XVY 4
XYZ 4

Is there a way to do this

any suggestions/inputs would be helpful

Thanks

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 Larsson
Helsingborg, Sweden
Go to Top of Page

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 All
Select 'ABC', 3 Union All
Select 'XVY,XYZ', 4


Select [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 @Table
on Number <= Len(',' + Name + ',') AND
SubString(',' + Name + ',' , Number - 1, 1) = ','
AND CharIndex(',' , ',' + Name + ',' , Number) - Number > 0

Order by [key]

--Output

Key INROW
----------- ------
2 ACS
2 AMS
3 ABC
4 XVY
4 XYZ


Chirag

http://chirikworld.blogspot.com/
Go to Top of Page

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)
)
AS

BEGIN
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

RETURN
END
This is the actual code to perform your task
-- Prepare test data
DECLARE @Test TABLE ([Name] VARCHAR(1000), [Key] INT)

INSERT @Test
SELECT 'ACS, AMS', 2 UNION ALL
SELECT 'ABC', 3 UNION ALL
SELECT 'XVY, XYZ', 4

-- Stage the data
DECLARE @CurrentName VARCHAR(1000),
@Key INT,
@MaxName VARCHAR(1000)

SELECT @CurrentName = MIN([Name]),
@MaxName = MAX([Name])
FROM @Test

DECLARE @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 output
SELECT * FROM @Stage



Peter Larsson
Helsingborg, Sweden
Go to Top of Page

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 ?

Chirag

http://chirikworld.blogspot.com/
Go to Top of Page

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 Larsson
Helsingborg, Sweden
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2006-10-10 : 09:53:17

http://www.sqlteam.com/item.asp?ItemID=2652

Madhivanan

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

- Advertisement -