This is a quick note to show you how to convert a given comma delimited string into a database table:
Given: "Title,Forenames,Surname" Return: ID Value ------ ---------------- 1 Title 2 Forenames 3 Surname Note the below example omits the ID column and just leaves VALUE.
- Given: "Title,Forenames,Surname"
- Return:
- ID Value
- ------ ----------------
- 1 Title
- 2 Forenames
- 3 Surname
- Note the below example omits the ID column and just leaves VALUE.
Why?
Do we need a reason?
How?
For SQL Server 2005 or later, using T-SQL:
DECLARE @myStringToParse VARCHAR(max), @myXML XML; SET @myStringToParse = 'Title,Forenames,Surname'; SELECT @myXml = CONVERT(xml,'<root><s>' + REPLACE(@myStringToParse,',','</s><s>') + '</s></root>'); SELECT [Value] = T.c.value('.','varchar(20)') FROM @myXml.nodes('/root/s') T(c);
- DECLARE @myStringToParse VARCHAR(max), @myXML XML;
- SET @myStringToParse = 'Title,Forenames,Surname';
- SELECT @myXml = CONVERT(xml,'<root><s>' + REPLACE(@myStringToParse,',','</s><s>') + '</s></root>');
- SELECT [Value] = T.c.value('.','varchar(20)') FROM @myXml.nodes('/root/s') T(c);