DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 2 of 2
  1. #1
    JonathanJ Guest

    How can i select the substring in a string and put it into another field in MS SQL server 2000?


    Dear sql ace,
    How can i select the substring in a string and put it into another field
    in MS SQL server 2000?
    For Example, how can i select only "NewYorkCity" in a address field "44WestFourthStreet,NewYorkCity,NY10012"
    and put it into another field? because i have to seprate the ONE address
    field into few fields, such as zip code, street, city and state.

    Best Regards,
    Jonathan

  2. #2
    Richard Guest

    Re: How can i select the substring in a string and put it into another field in MS SQL server 2000?


    Hi,

    It seems that the token that separates the substring in your string is a
    comma. You can find the location of the comma's by using functions like :
    CHARINDEX or PATINDEX. Then you can use the data in between the commas by
    using the substring function indicating where to start and where to stop.

    i.e.

    SELECT CHARINDEX(',', AddresField),
    -- this gives you the index of the first comma

    CHARINDEX('.', cname, CHARINDEX('.', cname)+1),
    -- this gives you the index of the second comma from the beginning

    SUBSTRING( AddressField , 1 , CHARINDEX(',', AddressField) - 1),
    -- This gives you the string from the beginning until the first comma.

    now you must be able to find the next string within the first and the second
    comma.

    cheers
    Richard



    "JonathanJ" <twcrazydruid@yahoo.com.tw> wrote:
    >
    >Dear sql ace,
    > How can i select the substring in a string and put it into another field
    >in MS SQL server 2000?
    > For Example, how can i select only "NewYorkCity" in a address field "44WestFourthStreet,NewYorkCity,NY10012"
    >and put it into another field? because i have to seprate the ONE address
    >field into few fields, such as zip code, street, city and state.
    >
    >Best Regards,
    >Jonathan



Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links