Solved

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

Posted on 2011-02-20
7
1,383 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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:ewangoya
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
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.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

914 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

19 Experts available now in Live!

Get 1:1 Help Now