Results 1 to 3 of 3

Thread: how to Separate address1 and address2 from address

  1. #1
    Join Date
    Oct 2002
    Location
    queens
    Posts
    139

    how to Separate address1 and address2 from address

    Hi all smart people,

    TableA has a column called address.
    values for example:

    address
    --------------------------------------
    107 31 118th street apt#2
    1717 east 16th st apt 3e
    attn: sales dept po box 398

    TableB has two fields called address1 and address2.

    Values need to be in there:

    address1 address2
    ------------------------------------
    107 31 118th street apt#2
    1717 east 16th st apt 3e
    attn: sales dept po box 398

    How can I make it happen? Please help.

    Thanks,
    Jannat.

  2. #2
    Join Date
    Sep 2002
    Location
    Fantasy
    Posts
    4,254
    it will be easy if it has a definite pattern. Only human can identify a pattern. if it is a variable pattern like this then we have to write a procedure to identify all known unique patterns.

    You can try the following and fix the bad rows later.

    create table addresstable (address varchar(100))

    insert into addresstable select "107 31 118th street apt#2"
    insert into addresstable select "1717 east 16th st apt 3e"
    insert into addresstable select "attn: sales dept po box 398"


    select left(address,6) as address1, substring(address,6,93) as address2 from addresstable

  3. #3
    Join Date
    Nov 2002
    Location
    DE
    Posts
    246
    Or you make it a bit more dynamic and split after at the position of the 2nd blank:

    SELECT LEFT (address, CharIndex (' ', address, CharIndex (' ', address)+1)-1)
    , Right (address, Len (address) - CharIndex (' ', address, CharIndex (' ', address)+1))
    However, as MAK said, you will have to check the result manually to find any record which does not follow the pattern...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •