Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2011-02-20
7
Medium Priority
?
1,390 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 2000 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

Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

Question has a verified solution.

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

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
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.

886 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