create a Field for City and State that is in one field in DB2

Posted on 2011-04-22
Last Modified: 2012-05-11
So the city and state is in one field in the DB2
what would be the sybtex in SQL 2008 R2 to start on the far right
go back two positions (the State in two positions)
and create that as its own field?
Question by:jaymz69
    LVL 9

    Expert Comment

    select right(fieldname, 2) from ...

    Author Comment

    so that would take the state out and into its own field?

    ex.  'SOMEWHERE       ST'

    needs to be 'SOMEWHERE'     'ST'
    LVL 40

    Accepted Solution

    try like this.
    declare @table table (col varchar(100))
    insert @table values ('SOMEWHERE       ST')
    select col,
           RTRIM(REVERSE(SUBSTRING(REVERSE(col),3,LEN(col)))) FirstPart,
           REVERSE(LEFT(REVERSE(col),2)) SecondPart
      from @table
    col	FirstPart	SecondPart

    Open in new window


    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    After restoring a Microsoft SQL Server database (.bak) from backup or attaching .mdf file, you may run into "Error '15023' User or role already exists in the current database" when you use the "User Mapping" SQL Management Studio functionality to al…
    In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now