Please provide a query that parses out the 3rd value out of this column.

0 votes
asked Nov 17, 2015 in T-SQL by Sunain (420 points)
  1. Suppose multiple values are stored in a single column of data separated by commas.  Please provide a query that parses out the 3rd value out of this column.  You can assume all rows will contain at least 4 values in this field. 

Example Column Data:

Red,Blue,Green,Yellow

1 Answer

0 votes
answered Nov 17, 2015 by darsh (3,020 points)

Hey Sunian,

Here we go 

DECLARE @s as varchar(80)
SET @s = 'Red,Blue,Green,Yellow'
Select 
    SUBSTRING (@s,
    CHARINDEX(',',@s,CHARINDEX(',',@s)+1)+1,
    CHARINDEX(',',@s,CHARINDEX(',',@s,CHARINDEX(',',@s)+1)+1)
          -CHARINDEX(',',@s,CHARINDEX(',',@s)+1)-1)

Welcome to I Can Crack, where you can ask questions and receive answers from other members of the community.

68 questions

47 answers

3 comments

5,318 users

...