• Sean Lange (12/19/2013)


    Everyone on this thread should take a look at the link in my signature for splitting strings. In there you will find a number of ways to split a string including the while loop and xml methods. As you continue reading you will find a tally table set based splitter. It will blow the doors off all the above methods for performance.

    Another method that is super duper fast is Dwain Camp's pattern splitter. You can find it here. http://www.sqlservercentral.com/articles/String+Manipulation/94365/[/url]

    I appreciate the vote of confidence Sean, but I think in this case I'd opt for something simpler like this:

    WITH SampleData (StartTime) AS

    (

    SELECT '7:00 AM <br/> 12:30 PM'

    UNION ALL SELECT '11:00 AM <br/> 2:30 PM'

    )

    SELECT StartTime, StartTimes

    FROM SampleData a

    CROSS APPLY

    (

    SELECT REPLACE(StartTime, ' <br/> ', ' ')

    ) b (st)

    CROSS APPLY

    (

    VALUES(LEFT(st, CHARINDEX('M', st))),(STUFF(st, 1, CHARINDEX('M', st)+1, ''))

    ) c (StartTimes)

    The CROSS APPLY VALUES approach to UNPIVOT is explained in the first article in my signature links.

    BTW. Last name is "Camps" so possessive would be Camps' 😀


    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