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 / 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 |
what is the cursor and use of cursor in pl/sql ?
What is an inconsistent dependency?
What is foreign key in sql with example?
what does it mean to have quoted_identifier on? What are the implications of having it off? : Sql dba
Are sql views compiled?
What is record in pl sql?
What is an Exception ? What are types of Exception ?
Can procedure in a package be overloaded?
What is difference between TRUNCATE & DELETE?
16 Answers Ahn Infotech, CitiGroup, ICICI, PreVator, Saama Tech, SkyTech, TCS,
what is a join? : Sql dba
What is compilation error in pl sql?
what is a sub query?how will you calculate working days in a month using sub query?
Oracle (3259)
SQL Server (4518)
MS Access (429)
MySQL (1402)
Postgre (483)
Sybase (267)
DB Architecture (141)
DB Administration (291)
DB Development (113)
SQL PLSQL (3330)
MongoDB (502)
IBM Informix (50)
Neo4j (82)
InfluxDB (0)
Apache CouchDB (44)
Firebird (5)
Database Management (1411)
Databases AllOther (288)