Have a look at example (using
CTE[
^]):
DECLARE @data TABLE(ID INT IDENTITY(1,1), SomeData NVARCHAR(30))
INSERT INTO @data (SomeData)
VALUES('A,B,C,')
INSERT INTO @data (SomeData)
VALUES('D,E,F,G,')
INSERT INTO @data (SomeData)
VALUES('H,I,')
INSERT INTO @data (SomeData)
VALUES('J,K,L,M,')
INSERT INTO @data (SomeData)
VALUES('N,O,P,')
INSERT INTO @data (SomeData)
VALUES('Q,R,S,')
INSERT INTO @data (SomeData)
VALUES('T,U,V,')
INSERT INTO @data (SomeData)
VALUES('X,Y,Z,')
DECLARE @tmp TABLE (ID INT, LETTER NVARCHAR(1))
;WITH CTE AS
(
SELECT ID, SomeData, LEFT(SomeData, CHARINDEX(',', SomeData)-1) AS Letter, RIGHT(SomeData, LEN(SomeData) - CHARINDEX(',', SomeData)) AS Remainder
FROM @data
WHERE CHARINDEX(',', SomeData)>1
UNION ALL
SELECT ID, SomeData, LEFT(Remainder, CHARINDEX(',', Remainder)-1) AS Letter, RIGHT(Remainder, LEN(Remainder) - CHARINDEX(',', Remainder)) AS Remainder
FROM CTE
WHERE CHARINDEX(',', Remainder)>1
)
INSERT INTO @tmp (ID, Letter)
SELECT ID, Letter
FROM CTE
ORDER BY ID, Letter
SELECT *
FROM @tmp
Result:
ID Letter
1 A
1 B
1 C
2 D
2 E
2 F
2 G
3 H
3 I
4 J
4 K
4 L
4 M
5 N
5 O
5 P
6 Q
6 R
6 S
7 T
7 U
7 V
8 X
8 Y
8 Z
Remember, similar query will be proper only if
complexion
field conatain (comma) as a last sign in a field.