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.



need to split a string into seperate values. eg. col1 col2 ---------- 100 - 'a,b,c&..

Answer / 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

More SQL PLSQL Interview Questions

With out using count() function. How to the find total number of rows in a table?

6 Answers  


What is meant by <> in sql?

0 Answers  


explain about mysql and its features. : Sql dba

0 Answers  


How many types of triggers are there in pl sql?

0 Answers  


Why do we need view in sql?

0 Answers  






what is the purpose of update command in oracle?

7 Answers   MBT,


What is the Diff b/w Constraints and Trigeer

4 Answers   HCL,


What is sql dialect?

0 Answers  


What is pessimistic concurrency control? : Transact sql

0 Answers  


what is the difference difference between procedure and packages

2 Answers   3i Infotech, Oracle,


What is use of trigger?

0 Answers  


which types of join is used in sql widely? : Sql dba

0 Answers  


Categories