Home Forums SQL Server 2008 SQL Server Newbies Creating a new column and inserting data on it from an existing column RE: Creating a new column and inserting data on it from an existing column

  • Something like this perhaps?

    WITH SampleData (MyString) AS

    (

    SELECT 'this,guy (Chicago)'

    )

    SELECT MyString

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

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

    ,C3=RTRIM(MAX(CASE WHEN ItemNumber = 5 THEN Item END))

    FROM SampleData a

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

    WHERE [Matched]=1

    GROUP BY MyString;

    PatternSplitCM can be found in the 4th article in my signature links.

    You may have to mess with it a bit if your actual data is not as clean as your sample.


    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