Solved

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

Posted on 2011-02-20
7
1,378 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
Comment Utility
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
Comment Utility
Thanks that works but i need it to be able to split many CSV e.g 6
0
 

Author Comment

by:Jimmy_inc
Comment Utility
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
Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

 
LVL 26

Accepted Solution

by:
tigin44 earned 500 total points
Comment Utility
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:ewangoya
Comment Utility

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
Comment Utility
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
Comment Utility
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

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Join & Write a Comment

Suggested Solutions

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
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…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

762 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now