EDIT: Your original post made it sound as if you were trying to retrieve multiple columns from a comma separated list, not the other way around. The code below was intended to reverse that process.
=================================================================================
I modified some code I had for a similar problem: let me know if this helps. As noted in the comments, this requires a table called Numbers: I put it as a temp table and loaded it in my script below, but you'd be better off making it permanent and loading it once.
-- Test Data
CREATE TABLE Contacts
(
Contacts_ID INT IDENTITY(1,1) NOT NULL,
First_Name VARCHAR(100) NOT NULL,
Last_Name VARCHAR(100) NOT NULL,
Mobile_Phones VARCHAR(100) NULL,
Office_Phones VARCHAR(100) NULL
)
INSERT INTO Contacts (First_Name, Last_Name, Mobile_Phones, Office_Phones)
SELECT 'Amancio', 'Ortega', '0501111111,0502222222', '031111111,032222222' UNION
SELECT 'Avi', 'Zohar', '0503333333', '033333333' UNION
SELECT 'Beat', 'Hirt', '0504444444,0505555555', NULL UNION
SELECT 'Ben', 'Gurion', '0501234567,05076545321', '034444444,035555555' UNION
SELECT 'Dany', 'Azriel', '0506764879,05065587436', '034847968'
-- Numbers table (for use in the formula): I would recommend creating a permanent table and populating it once. Make sure your count is higher than the length of your phone number columns.
CREATE TABLE #Numbers (Number INT NOT NULL)
INSERT INTO #Numbers (Number) SELECT 1
DECLARE @Limit INT
DECLARE @Counter INT
SET @Limit = 101
SET @Counter = (SELECT MAX(Number) FROM #Numbers)
WHILE @Counter < @Limit
BEGIN
INSERT INTO #Numbers (Number) SELECT MAX(Number) + 1 FROM #Numbers
SET @Counter = (SELECT MAX(Number) FROM #Numbers)
END
--SELECT * FROM #Numbers
-- Retrieve the information
SELECT
Contacts_ID,
First_Name,
Last_Name,
PhoneType,
CAST(LTRIM(RTRIM(NULLIF(SUBSTRING(',' + PhoneNumber + ',' , Number , CHARINDEX(',' , ',' + PhoneNumber + ',' , Number) - Number) , ''))) AS VARCHAR(10)) PhoneNumber
FROM #Numbers,
(SELECT
Contacts.Contacts_ID,
Contacts.First_Name,
Contacts.Last_Name,
Contacts.Mobile_Phones,
Contacts.Office_Phones
FROM Contacts
) P
UNPIVOT
(PhoneNumber FOR PhoneType IN
(
Mobile_Phones, Office_Phones
)) AS UP
WHERE
Number <= Len(',' + PhoneNumber + ',') AND SubString(',' + PhoneNumber + ',' , Number - 1, 1) = ',' AND
CharIndex(',' , ',' + PhoneNumber+ ',' , Number) - Number > 0
-- Clean up
/*
DROP TABLE Contacts
DROP TABLE #Numbers
*/