Parse field containing tab-delimited data into multiple fields

Posted on 2009-02-10
Last Modified: 2012-05-06
I have one field in a table that contains name and address data and within that one field, the name, address1, address2, city, state, ZIP are each separated by the tab character.  How do I parse the data from that one field into a temporary table containing separate fields for each portion of the name/address?
Question by:johntmcii
    LVL 25

    Expert Comment

    You will need a UDF (user defined function) which splits a single varchar string and returns a table ..
    LVL 25

    Expert Comment

    What version is the SQL backend, if its SQL 2005 or later you can use a CTE solution..

    Author Comment

    SQL 2005
    LVL 25

    Accepted Solution

    Here's a CTE solution with a template table TmpDetails mirroring your table -
    if object_id('TmpDetails','U') IS NOT NULL
    	drop table TmpDetails
    create table TmpDetails (contactid int not null, contactdetails nvarchar(4000) null)
    insert into TmpDetails
    select 1, 'Aname	Aaddress1	Aaddress2	Acity	Astate	AZIP' union all
    select 2, 'Bname	Baddress1		Bcity	Bstate	BZIP' union all
    select 3, 'Cname			Ccity	Cstate	CZIP' union all
    select 4, 'Dname				Dstate	' union all
    select 5, 'Ename					'
    DECLARE @sep char(1)
    SET	@sep = char(9)  -- Tab character!
    ;WITH CTE (Id, Value, Remnant, Field)
            SELECT contactid, Code = contactdetails, Remnant = contactdetails, Field = 0
            FROM	TmpDetails
            UNION ALL
            SELECT  Id,
                    Code = LEFT(Remnant, ISNULL(pos, remsize)),
                    Remnant = RIGHT(Remnant, ISNULL(remsize - pos, 0)),
                    Field = Field + 1
    	                pos = NULLIF(PATINDEX('%' + @sep + '%', Remnant), 0),
    	                remsize = LEN(Remnant),
    	                CTE.Id, CTE.Remnant, CTE.Field
    	         FROM CTE
    	         INNER JOIN TmpDetails on TmpDetails.contactid = CTE.Id
    	         WHERE Remnant > ''
    	        ) Tmp
    -- Statement that executes the CTE
    SELECT Id, [1] as Fld1,[2] as Fld2, [3] as Fld3, [4] as Fld4, [5] as Fld5
    FROM (SELECT Id, Field, Value FROM CTE WHERE Field > 0) CTE
    PIVOT (MAX(Value) FOR Field IN ([1],[2],[3],[4],[5])) PVT

    Open in new window


    Author Closing Comment

    Thanks, that's what I needed.

    Featured Post

    Courses: Start Training Online With Pros, Today

    Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

    Join & Write a Comment

    JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
    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.
    Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
    Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

    746 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

    15 Experts available now in Live!

    Get 1:1 Help Now