I have a table in a database (that I am not allowed to modify) that is setup in the following manner:
ID Value Column x...
0 Apple: GDGolden Delicious, Date Picked: 5/5/2012, Color: Yellow
1 Apple: GSGranny Smith, Date Picked: 3/24/2010, Color: Green
2 Apple: RDRed Delicious, Date Picked: 1/15/2012, Color: Red
3 Random Data, Apple: BRBrayburn, Date Picked: 2/15/2012, Color: Mixed
ID is an INT and Value is a VarChar, delimited by commas between data.
For every row in the table, I need to pull out a substring of what is listed, in this case, the abbreviation for each fruit variety (i.e. GD, GS, RD, BR) and place it into a temporary table that contains all of the other columns from that table, along with the substring but no other data from the Value column. Each substring abbreviation in the table is exactly two characters, although as you see on the last row, it does not always appear at the beginning of the column.
In my reading, it sounds like I’d have to use a cursor to do this — but I’d like to avoid that if at all possible. Is there anyway to accomplish this without using a cursor, through a case statement or subquery perhaps?
The end result would look like this:
ID Value Column x Column y...
0 GD
1 GS
2 RD
3 BR
Thanks in advance.
In the event, your record does not say
Apple:, you can look for the first::See SQL Fiddle with Demo