Solved

How to Parse Comma-Separated Text into Multiple Fields using  MS SQL Server

Posted on 2011-02-20
7
1,387 Views
Last Modified: 2012-05-11
I need to achieve the same functionality described in http://support.microsoft.com/kb/95608 - method2 but for SQL Server 2008.
0
Comment
Question by:Jimmy_inc
  • 4
  • 2
7 Comments
 
LVL 26

Expert Comment

by:tigin44
ID: 34937466
use built in SQL function like

DECLARE @text      VARCHAR(MAX);
SET @text = 'Smith,John';
SELECT LEFT(@text, CHARINDEX(',', @text ) - 1) AS lastName,
      RIGHT(@text, LEN(@text) - CHARINDEX(',', @text )) AS firstName
0
 

Author Comment

by:Jimmy_inc
ID: 34937517
Thanks that works but i need it to be able to split many CSV e.g 6
0
 

Author Comment

by:Jimmy_inc
ID: 34937539
Rather.. thi will be dynamic, depending on how many CSV are used, also it is likely this results will need to be joined to itself.
0
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
LVL 26

Accepted Solution

by:
tigin44 earned 500 total points
ID: 34937568
this may help you


DROP TABLE Sample
CREATE TABLE Sample(
	ID		int,
	someVal varchar(10),
	anotVal varchar(10)
);


DECLARE @text	VARCHAR(MAX);
DECLARE @numCols int;
DECLARE @i		int;
DECLARE @col1	int;
DECLARE @col2	varchar(10);
DECLARE @col3	varchar(10);

SET @text = '1,aaa,aba,2,bbb,bab,3,ccc,cac,4,ddd,dad,'
SET @numCols = 3;
SET @i = 1

WHILE LEN(@text) > 0
BEGIN
select @i, @text, LEFT(@text, CHARINDEX(',', @text ) - 1) 
	IF @i = 1 
		SELECT @col1 = CAST(LEFT(@text, CHARINDEX(',', @text ) - 1) AS int) 
	IF @i = 2 
		SELECT @col2 = LEFT(@text, CHARINDEX(',', @text )-1) 
	IF @i = 3 
		SELECT @col3 = LEFT(@text, CHARINDEX(',', @text )-1) 

	SET @i = @i + 1;
	
	IF @i-1 = @numCols
	BEGIN
		INSERT INTO Sample
		VALUES(@col1, @col2, @col3);
		SET @i = 1;
	END	
	SELECT @text = RIGHT(@text, LEN(@text) - CHARINDEX(',', @text ))
END

select * from Sample

Open in new window

0
 
LVL 32

Expert Comment

by:Ephraim Wangoya
ID: 34940194

You can simple use bulk insert to import the csv data into your tables. This will split the data for you according to the delimiter you choose.
If you want to eidt the data or joins, you can insert into temporary tables too


CREATE TABLE #NAMES 
( 
    ID INT, 
    FIRSTNAME VARCHAR(32), 
    LASTNAME VARCHAR(32)
)

BULK INSERT #NAMES 
    FROM 'c:\file.csv' 
    WITH 
    ( 
        FIRSTROW = 2,  --IF THE FIRST ROW IS COLUMN HEADERS
        FIELDTERMINATOR = ',', 
        ROWTERMINATOR = '\n' 
    )

Open in new window

0
 

Author Comment

by:Jimmy_inc
ID: 35066067
Sorry mate I've been on holiday!!

I need to  SQL which is compatible with SQL 2000 - SQL 2008 so is this a reason? why I am getting:

******************************************************
Server: Msg 170, Level 15, State 1, Line 9
Line 9: Incorrect syntax near 'max'.
******************************************************

I'm new to MS SQL Server

Can i step through the code or is there a way to use a locals window ?

thanks



0
 

Author Comment

by:Jimmy_inc
ID: 35066915
Also tigin44 I need the code to reference a field so am I to replace @text with  the fields name?
I will try this...
0

Featured Post

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
find SQL job run average duration 24 57
Need help with a query 14 39
Trigger C# code inside the SQL Server 6 36
SQL Server remove line breaks and tabbed 2 9
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question