Hello Ankit,
Quite simple :)
You've managed to find the first comma by the CHARINDEX,
and your problem is to find the others (in your case the second).
What you can do is start the search for a starting index, after the first appearance.
See this section of
Vishal's post on sqlandme:
DECLARE @string VARCHAR(128)
SET @string = 'CHARINDEX searches for the string for the ' +
'first occurrence of a specified character/string'
SELECT CHARINDEX('Char', @string) AS 'Position',
CHARINDEX('Char', @string, 2) AS 'Position'
Result Set:
Position Position
———– ———–
1 - 75
(1 row(s) affected)
In the above example, the first columns returns 1 as the string started with 'CHAR…', while in the second column the search was started from 'HARINDEX…', which resulted in returning the second occurrence of 'CHAR'.
Searches are based on the collation of the data. CHARINDEX() has performed a case-insensitive search as my current collation is set to Latin1_General_CI_AI. To perform a case-sensitive search here we can apply Latin1_General_CS_AI collation to @string:
DECLARE @string VARCHAR(128)
SET @string = 'CHARINDEX searches for the string for the ' +
'first occurrence of a specified character/string'
SELECT CHARINDEX('char', @string COLLATE Latin1_General_CS_AI)
AS 'Position'
Result Set:
Position
———–
75