Breaking up a string of text

  • Hello again,

    I have some very dirty data here. I'm working on a Customers table which I plan to clean up and insert into a new table. Currently, the table looks like this:

    LastName, FirstName, MiddleInt

    -----------------------------------------

    Smith,John,Q * *

    So "Smith,John,Q" IS the last name and "*" are stored in the First Name and Middle Int fields.

    Yuk.

    I am able to just select the last name based on the comma like so:

    [font="Courier New"]LEFT(LastName,(CHARINDEX(',',LastName))) AS LastNameOnly[/font]

    However, I am having trouble pulling out the first name. Hell, at this point, I'd be fine with it if first name contained any possible Middle Int. I'd be fine if FirstName was "John,Q". I can clean that up in a second step.

    I'd be grateful if someone could help just getting the "First Name" out of the string. I've tried various methods of SUBSTRING, LEN, and what not. Just can't seem to connect the dots....

    Thanks

  • This article should be handy:

    DelimitedSplit8K[/url]

    Create the DelimitedSplit8K function as detailed in the article, and CROSS APPLY it to your comma-delimited data, like so:

    SELECT * FROM YourTable

    CROSS APPLY YourDatabase.YourSchema.DelimitedSplit8K(YourTable.Lastname,',')

    Replace the "Your" bits with your actual tables. This will show you how the original string, and how it will be split out based on the delimiter (in this case, the comma at the end of the DelimitedSplit8K call). From there, you'll need to update each part of the name with the split-out entries you end up with from using DelimitedSplit8K. This can be clarified further as needed, but it should provide a good starting point.

    - 😀

  • This seems to mostly work:

    RIGHT(LastName,(LEN(LastName))-CHARINDEX(',',LastName)) As FirstNameOnly

    I needed to use Right Trim but I didn't know how many positions to trim to. So, I did this:

    Right(LastName, C)

    A = Length of whole string

    B = Position of Comma

    C = A-B (Remaining chars)

  • try

    select

    substring( lastname, charindex(',', lastName )+1,

    (charindex (',', lastName, charindex(',', lastName )+1 )-

    charindex(',', lastName ))-1

    )

  • My advice is plan for the worst and use a pattern split function like the one you'll find in the 4th link in my signature.

    WITH SampleData (Name) AS

    (

    SELECT 'Smith,John,Q'

    UNION ALL SELECT 'Fedders-Smith,John,Q'

    UNION ALL SELECT 'Smith, John Q'

    UNION ALL SELECT 'Smith,John Quincy'

    UNION ALL SELECT ' Smith,John Quincy'

    )

    SELECT Name

    ,LastName=MAX(CASE ItemNumber WHEN 1 THEN Item END)

    ,FirstName=MAX(CASE ItemNumber WHEN 3 THEN Item END)

    ,LastName=MAX(LEFT(CASE ItemNumber WHEN 5 THEN Item END, 1))

    FROM

    (

    SELECT Name, ItemNumber, Item

    FROM SampleData a

    CROSS APPLY dbo.PatternSplitCM(LTRIM(Name), '[\-a-zA-Z]') b

    WHERE [Matched]=1

    ) a

    GROUP BY Name;


    My mantra: No loops! No CURSORs! No RBAR! Hoo-uh![/I]

    My thought question: Have you ever been told that your query runs too fast?

    My advice:
    INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
    The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.

    Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply