[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


SQL 2005 - Parse CSV field

Posted on 2007-08-05
Medium Priority
Last Modified: 2008-09-05
I have a field (f2) in a tblImportError that has names, with no spaces, seperated by commas (last,first,middle)

This code works perfectly, until we come to a name that has a first name with one character and no middle name.  It will error out.
Changing the SUBSTRING(REVERSE(f2), 2, 1) to SUBSTRING(REVERSE(f2), 1, 1) in the key2 line leaves commas after the last name.

update tblImportError
    Set key2 = SUBSTRING(f2, CHARINDEX(',', f2) + 1, LEN(f2) - (CHARINDEX(',', f2) + 0) - CASE WHEN  
         SUBSTRING(REVERSE(f2), 2, 1) = ',' THEN 1 ELSE 0 END) -- firstname
       ,key3 = LEFT(f2, CHARINDEX(',', f2) - 1) -- lastname
       ON tblImportError.key1 = tblImport1.f1

Any help is appreciated!!!
Question by:fhcdaver
LVL 42

Expert Comment

ID: 19636361
update tblImportError
 set key2=substring(f2+ ',,',1,charindex(',',f2+',,')-1) --lastname
,set key3=substring(substring(name+ ',,',charindex(',',name+',,')+1,len(name+',,')-charindex(',',name+',,')+1)
,1,charindex(',',substring(name+ ',,',charindex(',',name+',,')+1,len(name+',,')-charindex(',',name+',,')+1))-1
) --firstname

Accepted Solution

kenhaley earned 2000 total points
ID: 19636496
Here's a great application for a user-defined function.  This is one that I wrote long ago, and have used many times...

create function dbo.fn_Subfield(@string varchar(8000), @sep char(1), @count int)
    returns varchar(8000)
    -- Parses a delimited field, returning the desired subfield.
    -- Parameters:
    -- @string - the delimited field
    -- @sep - single-character separator (comma, period, etc.)
    -- @count - which subfield to return (1 = 1st, 2=2nd, etc.)
    declare @result varchar(8000)
    declare @ptr int

    while @count > 0 begin
        set @ptr = charindex(@sep, @string)
        if @ptr = 0 set @ptr = len(@string) + 1
        if @count = 1 break;
        set @string = substring(@string, @ptr+1, 8000)
        set @count = @count - 1
    set @result = substring(@string, 1, @ptr-1)    
    return @result

Using this function your problem would be solved as follows:

update tblImportError
    Set key2 = dbo.fn_Subfield(f2, ',', 2) -- firstname
       ,key3 = dbo.fn_Subfield(f2, ',', 1) -- lastname
       ON tblImportError.key1 = tblImport1.f1

Much easier to read, and you can just as easily use it to extract the 7th subfield from a 10-field delimited list, if you like.  

Hope this helps.

Author Comment

ID: 19642560
This was a fast and easy solution.
Thank you!

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

If you having speed problem in loading SQL Server Management Studio, try to uncheck these options in your internet browser (IE -> Internet Options / Advanced / Security):    . Check for publisher's certificate revocation    . Check for server ce…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?

834 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