all groups > sql server programming > october 2005 >
You're in the

sql server programming

group:

Comma-separated field


Comma-separated field Bils
10/23/2005 10:45:53 PM
sql server programming:
Hi,
table (contact) has a field categories with values like
row 1:
bil;jack;john;don
row 2:
bil;zub;sam;tom;jack

Ideally I would like to create a new table (split_table) with the
following values in each row without any duplicate. The

item NoOfOccurence
---- ----
bil 2
jack 2
john 1
don 1
zub 1
sam 1
tom 1

Does anyone has a simple code for a newbie using MS SQL 2000. I don't
mind using a stored procedure or cursor. I've seen solution but they
seem too complicated for me to even modify.

I appreciate any help and suggestion.
Thank you and kind regards
Bils
Re: Comma-separated field Dejan Sarka
10/24/2005 12:00:00 AM
Check the article at
http://www.windowsitpro.com/SQLServer/Article/ArticleID/25678/25678.html.

--
Dejan Sarka, SQL Server MVP
Mentor
www.SolidQualityLearning.com

[quoted text, click to view]

Re: Comma-separated field Syu
10/24/2005 12:00:00 AM
My solution is:

select * into #t from source
select * into #t1 from source where 0 = 1
while exists(select * from #t where len(row) > 0)
begin
insert into #t1 (row)
select substring(row,1,charindex(';',row + ';') - 1)
from #t
update #t set row = substring(row,charindex(';',row + ';') + 1,len(row))
end

select row as Item,count(*) as NoOfOccurence
from #t1
where len(row) > 0
group by row

drop table #t1
drop table #t

No proc, no cursor - is that you want?


--
endorsed by signature
*** Serg Yury ***

"Bils" <bjeewa@advsol.com> ???????/???????? ? ???????? ?????????:
news:1130132753.169888.314860@g44g2000cwa.googlegroups.com...
[quoted text, click to view]

Re: Comma-separated field Bils
10/24/2005 12:12:08 AM
simply brilliant. Thank you very much guys for your assistance
Kind regards
Bils
Re: Comma-separated field Rebecca York
10/24/2005 11:04:05 AM
IF OBJECT_ID('tempdb..#tmp') IS NOT NULL DROP TABLE #tmp
IF OBJECT_ID('tempdb..#tmp2') IS NOT NULL DROP TABLE #tmp2

SELECT
Row
, Data
INTO
#tmp
FROM
(
SELECT TOP 0 NULL AS Row , NULL AS Data
UNION ALL SELECT 1 , 'bil;jack;john;don'
UNION ALL SELECT 2 , 'bil;zub;sam;tom;jack'
) Data


SELECT
Row
, NULLIF( SUBSTRING( #tmp.Data , n , CHARINDEX( ';' , #tmp.Data + ';' ,
n ) - n ) , '' ) AS Item
INTO
#tmp2
FROM
#tmp
INNER JOIN
tblNumbers
ON
tblNumbers.n BETWEEN 1 AND DATALENGTH( #tmp.Data )
AND
SUBSTRING( ';' + #tmp.Data , n , 1 ) = ';'

SELECT
Item
, COUNT(*) AS NoOfOccurence
FROM
#tmp2
GROUP BY
Item
ORDER BY
NoOfOccurence DESC
, Item


This should be quicker than using a cursor/loop.


[quoted text, click to view]

Re: Comma-separated field Rebecca York
10/24/2005 11:07:38 AM
Sure, no cursor - but if the data contains 50,000 records with 100 items per
record, you'll have 5,000,000 record updates to the tempdb and it will take
forever.

This is not a scalable solution.

[quoted text, click to view]

Re: Comma-separated field Rebecca York
10/24/2005 11:55:39 AM
You can also do without the temporary table by using a subquery.

.... replace #tmp with your datasource

SELECT
Item
, COUNT(*) AS NoOfOccurence
FROM
(
SELECT
Row
, NULLIF( SUBSTRING( #tmp.Data , n , CHARINDEX( ';' , #tmp.Data + ';' ,
n ) - n ) , '' ) AS Item
FROM
#tmp
INNER JOIN
tblNumbers
ON
tblNumbers.n BETWEEN 1 AND DATALENGTH( #tmp.Data )
AND
SUBSTRING( ';' + #tmp.Data , n , 1 ) = ';'
) Data
GROUP BY
Item
ORDER BY
NoOfOccurence DESC
, Item


[quoted text, click to view]

Re: Comma-separated field Syu
10/25/2005 7:32:11 AM
First of all: what is tblNumbers?

I suppose:

SELECT n INTO tblNumbers
FROM
(
SELECT TOP 0 NULL AS n
UNION ALL SELECT 1
UNION ALL SELECT 2
-- ...
-- ... to DATALENGTH(#tmp.Data) ????
) Data

I agree - your solution more elegant, if not to take into account creation
of table tblNumbers...

--
endorsed by signature
*** Serg Yury ***

"Rebecca York" <rebecca.york {at} 2ndbyte.com> ÓÏÏÂÝÉÌ/ÓÏÏÂÝÉÌÁ × ÎÏ×ÏÓÔÑÈ
ÓÌÅÄÕÀÝÅÅ: news:435ca9c0$0$142$7b0f0fd3@mistral.news.newnet.co.uk...
[quoted text, click to view]


Re: Comma-separated field Rebecca York
10/25/2005 1:48:43 PM
Everyone needs tblNumbers :)

It contains numbers 1 to 100,000.

To populate it:-

USE UtilsDatabase

CREATE TABLE
dbo.tblNumbers
(
n INT
, CONSTRAINT PK_dbo_tblNumbers PRIMARY KEY CLUSTERED ( n ASC )
)
GRANT SELECT ON dbo.tblNumbers TO PUBLIC


IF NOT EXISTS( SELECT n FROM dbo.tblNumbers )
INSERT INTO dbo.tblNumbers ( N )
SELECT TOP 0 NULL AS n
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



DECLARE @Loop TINYINT , @HowManyZeros TINYINT , @MAX_N INT

SELECT @Loop = 1 , @HowManyZeros = 5

WHILE @Loop < @HowManyZeros
BEGIN

SELECT @MAX_N = MAX( n ) FROM dbo.tblNumbers

INSERT INTO
dbo.tblNumbers ( n )
SELECT
n + n2 * @MAX_N AS N
FROM
dbo.tblNumbers
CROSS JOIN
( SELECT n AS n2 FROM dbo.tblNumbers WHERE n BETWEEN 1 AND 9 ) BaseTen

SELECT @Loop = @Loop + 1

END

DENY INSERT , UPDATE , DELETE ON dbo.tblNumbers TO PUBLIC







[quoted text, click to view]

AddThis Social Bookmark Button