Home Forums SQL Server 2008 T-SQL (SS2K8) Substring Query to pull firstname and lastname out of one column RE: Substring Query to pull firstname and lastname out of one column

  • well, one way is to use the delimitedSplit8K function to chop the string into bitesize peices.

    from there, you might need to cross apply, modifying the WHERE statement to limit it to teh data that's mostly correct.

    take alook at this example...you'll see how the name is chopped into 2 or 3 pieces per ID, based on your sample data.

    With mySampleData(Id,String)

    AS

    (

    SELECT 1,'HIST-AS-JEFFERY WINKEL-02/26/1976-07/06/2012' UNION ALL

    SELECT 2,'HIST-AS-JOSEPH WALDNER-07/07/1965-07/06/2012' UNION ALL

    SELECT 3,'HIST-AS-TERESA KLINE-10/09/1965-02/13/2012' UNION ALL

    SELECT 4,'HIST-HS-A. MARION GROSETH-11/10/1931-01/19/2009' UNION ALL

    SELECT 5,'HIST-HS-AARON ALFRED-07/06/1967-06/02/2010'

    )

    select

    mySampleData.*,

    ByDashes.*,

    BySpaces.*

    from mySampleData

    CROSS APPLY dbo.DelimitedSplit8K(mySampleData.String,'-') ByDashes

    CROSS APPLY dbo.DelimitedSplit8K(ByDashes.Item,' ') BySpaces

    WHERE ByDashes.ItemNumber = 3 --the 3rd dash group has the values we want to further split by spaces

    {edit}

    here's the article containing the DelimitedSplit8K funcrtion:

    http://www.sqlservercentral.com/articles/Tally+Table/72993/

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!