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

What is union?

817


can a stored procedure call itself or recursive stored procedure? : Sql dba

752


How do you write an inner join query?

694


Who developed sql?

758


What is primary key and foreign key?

719






Do ddl statements need commit?

726


Explain aggregate functions are available there in sql?

752


How to create a menu in sqlplus or pl/sql?

811


How to find 3rd highest salary of an employee from the employee table in sql?

793


What is a sql select statement?

766


What is procedure in pl sql?

727


What is mutating sql table?

826


What is a pl/sql block?

759


How do I make sql search faster?

768


How to run pl/sql statements in sql*plus?

760