A Little String Separation

  • Hello everyone

    I hope that you all had a nice weekend.

    I am working on cleaning up some data that I receive from an outside source. What?! Yes, I know.........

    I am receiving one column with delimited data, I know, another real shocker. The data is City, State PostalCode all together in one column. I am not very good at performing text manipulations, so I need a little help with this.

    The format of each is City, State PostalCode ie.... NEW LIBERTY, IA 52765-9420

    I would like to know how to separate each into its respective column. I know how to perform an update, I need help with breaking the string apart so that I get City in the CityName Column, State into the StateAbbrev Column and Zip into PostalCode column

    Thank You in advance for your help

    Andrew SQLDBA

  • All done by J. Moden:

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

    _____________________________________________
    "The only true wisdom is in knowing you know nothing"
    "O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!":-D
    (So many miracle inventions provided by MS to us...)

    How to post your question to get the best and quick help[/url]

  • Do what?

    I was hoping for something a little more simple that what is shown on that page.

    Thanks

    Andrew SQLDBA

  • AndrewSQLDBA (5/29/2012)


    Do what?

    I was hoping for something a little more simple that what is shown on that page.

    Thanks

    Andrew SQLDBA

    Do you want simple or fast? 😉

    Technically, if you can use CLR then that would be simple and fast. But a lot of places are still unable to use them.


    Forever trying to learn
    My blog - http://www.cadavre.co.uk/
    For better, quicker answers on T-SQL questions, click on the following...http://www.sqlservercentral.com/articles/Best+Practices/61537/
    For better, quicker answers on SQL Server performance related questions, click on the following...http://www.sqlservercentral.com/articles/SQLServerCentral/66909/

  • You mean that I cannot have both? 😀

    Fast of course. I take it if I want fast I should follow what was first sent.

    Andrew SQLDBA

  • AndrewSQLDBA (5/29/2012)


    You mean that I cannot have both? 😀

    Fast of course. I take it if I want fast I should follow what was first sent.

    Andrew SQLDBA

    As of the last time I checked, Jeff's string splitter is the fastest way to split text (less than 8000 characters) using purely t-sql. A CLR function will beat it, just about, but it's not an option in a lot of development environments. Paul wrote a CLR here.


    Forever trying to learn
    My blog - http://www.cadavre.co.uk/
    For better, quicker answers on T-SQL questions, click on the following...http://www.sqlservercentral.com/articles/Best+Practices/61537/
    For better, quicker answers on SQL Server performance related questions, click on the following...http://www.sqlservercentral.com/articles/SQLServerCentral/66909/

  • I think what is really making this more difficult for me is that the second delimiter is a blank space. I am not wrapping my head around that easily. There are only about 3000 rows, and this is a one time query.

    Thanks

    Andrew SQLDBA

  • No reason to use Jeff's splitter if the data format is constant:

    use tempdb

    go

    declare @test-2 varchar(125)

    SET @test-2 = 'barrington, il 60010-4020'

    SELECT RIGHT(@test, 10) AS zip, SUBSTRING(@test, CHARINDEX(',',@test)+2, 2) AS state, SUBSTRING(@test, 1, CHARINDEX(',',@test)-1) AS city

    Jared
    CE - Microsoft

  • AndrewSQLDBA (5/29/2012)


    I think what is really making this more difficult for me is that the second delimiter is a blank space. I am not wrapping my head around that easily. There are only about 3000 rows, and this is a one time query.

    Thanks

    Andrew SQLDBA

    So run a replace on the first deliminator + second deliminator to equal one deliminator 😀

    Then use Jeff's little bit of work.

    You could use an XML splitter, but it will be slower.


    Forever trying to learn
    My blog - http://www.cadavre.co.uk/
    For better, quicker answers on T-SQL questions, click on the following...http://www.sqlservercentral.com/articles/Best+Practices/61537/
    For better, quicker answers on SQL Server performance related questions, click on the following...http://www.sqlservercentral.com/articles/SQLServerCentral/66909/

  • Thank You to Everyone for all the great advice. Always learn something when I post here.

    Have a good one

    Andrew SQLDBA

  • SQLKnowItAll (5/29/2012)


    No reason to use Jeff's splitter if the data format is constant:

    use tempdb

    go

    declare @test-2 varchar(125)

    SET @test-2 = 'barrington, il 60010-4020'

    SELECT RIGHT(@test, 10) AS zip, SUBSTRING(@test, CHARINDEX(',',@test)+2, 2) AS state, SUBSTRING(@test, 1, CHARINDEX(',',@test)-1) AS city

    Very true, if you know the number of elements never changes this will work. I'd be interested to see a performance test vs Jeff's code on say 250 elements.


    Forever trying to learn
    My blog - http://www.cadavre.co.uk/
    For better, quicker answers on T-SQL questions, click on the following...http://www.sqlservercentral.com/articles/Best+Practices/61537/
    For better, quicker answers on SQL Server performance related questions, click on the following...http://www.sqlservercentral.com/articles/SQLServerCentral/66909/

Viewing 11 posts - 1 through 10 (of 10 total)

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