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
What are different functions in sql?
How to read/write files from pl/sql?
What is sql and how does it work?
Can primary key be changed?
what is not null constraint? : Sql dba
What is a schema sql?
What is the difference between the conventional and direct path loader? : aql loader
What is trigger in sql and its types?
Can triggers stop a dml statement from executing on a table?
what are dynamic queries in t-sql? : Transact sql
Inline the values in PL/SQL, what does it mean.?
Is it possible to update views?
What are the qualities of 2nf?
How do you determine the current isolation level? : Transact sql
Can I call a procedure inside a function?