need to split a string into seperate values.

eg.

col1 col2
----------
100 - 'a,b,c'
200 - 'a,x,b,d,e'
300 - 'c'

result:
value count
-------------
a - 2
b - 1
c - 2
etc.

Answer Posted / gopi muluka

DECLARE @STR VARCHAR(50),
@Char VARCHAR(10),@N INT, @CNT INT
DECLARE @TAB TABLE (VAL VARCHAR(30), CNT INT)

SET @STR='A,B,C,D,E,F,C,A,S,K,C,B'
SET @CHAR=''
SET @N=1
SET @CNT=1
WHILE @N>0
BEGIN
PRINT @STR
SET @CHAR=SUBSTRING(@STR,1,CHARINDEX(',',@STR)-1)
IF NOT EXISTS(SELECT 1 FROM @TAB WHERE VAL=@CHAR)
INSERT @TAB VALUES (@CHAR,@CNT)
SET @STR=SUBSTRING(@STR,CHARINDEX(',',@STR)+1,115)
IF CHARINDEX(@CHAR,@STR)>0
BEGIN
UPDATE @TAB
SET CNT=CNT+1
WHERE VAL=@CHAR
END
SET @N=CHARINDEX(',',@STR)
PRINT @N
END
SELECT * FROM @TAB

Is This Answer Correct ?    0 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

How can I make sql query run faster?

562


How do I view an execution plan in sql?

531


Are pl sql variables case sensitive?

598


How many types of relationship are there?

547


What is constant in pl sql?

519






What are the advantages of sql?

560


What are different types of sql commands?

514


Why are indexes and views important to an organization?

529


Explain the rollback statement?

578


What does select * from mean in sql?

1994


What are pl/sql cursor exceptions?

575


Can you inner join the same table?

527


What is mutating trigger?

585


What is a file delimiter?

556


How do you update a value in sql?

549