Results 1 to 8 of 8
  1. #1
    JamesBond is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Apr 2014
    Posts
    11

    Split a text string using a query and depending on its content


    Hi, I am relatively new to Access. I imported a very large csv file into a table. One field relates to phone numbers. Some records have 2 phone numbers others have only 1 phone number. Where there are 2 numbers they are split by a ":" Here is an example

    PhoneNos
    089 4070198
    087 3983530
    089 4843365
    086 1027601
    083 1987275
    087 3377219:+44 7909 947901
    087 3899103
    086 1047498
    089 9616322
    085 7799250:+44 7563 076923
    00894 962918:00894 962918


    I want to split the phone field into 2 fields. I tried using Left() and Mid(). However if the record has only 1 phone number I get an error message. Here is an example of the result

    FirstPhone MidPhone
    #Error 089 4070198
    #Error 087 3983530
    #Error 089 4843365
    #Error 086 1027601
    #Error 083 1987275
    087 3377219 +44 7909 947901
    #Error 087 3899103
    #Error 086 1047498
    #Error 089 9616322
    085 7799250 +44 7563 076923
    00894 962918 00894 962918


    This is the way I would like to see the data returned

    FirstPhone MidPhone
    089 4070198
    087 3983530
    089 4843365
    086 1027601
    083 1987275
    087 3377219 +44 7909 947901
    087 3899103
    086 1047498
    089 9616322
    085 7799250 +44 7563 076923
    00894 962918

    I would appreciate any help thank you in advance

  2. #2
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows XP Access 2003
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,614
    Can you tell us the name of the table and the name of the field
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  3. #3
    JamesBond is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Apr 2014
    Posts
    11
    Sorry I should have included those details.

    Table is: tbMaster
    Field is: Phones

  4. #4
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows XP Access 2003
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,614
    Try the following as the SQL of your query:
    SELECT tbMaster.Phones, IIf(InStr(1,[Phones],":")-1=-1,[Phones]) AS FirstPhone, IIf(IsNull([fp]),Right([Phones],InStrRev([Phones],":")),"") AS MidPhone FROM tbMaster;
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  5. #5
    JamesBond is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Apr 2014
    Posts
    11
    Hi Bob
    Thank you for that, it is almost there but not quite. As you can see below if there is just 1 number in the PhoneNos field it puts it in the FirstPhone field. However if there are 2 numbers in PhoneNos it does not put the first number in FirstPhone and it does not copy the full text of the second number into SecondPhone and in some cases it copies the ":" as part of the SecondPhone. Thank you for your help with this.

    PhoneNos FirstPhone SecondPhone
    089 9616322 089 9616322
    085 7799250:+44 7563 076923
    7563 076923
    00894 962918:00894 962918
    :00894 962918

  6. #6
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows XP Access 2003
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,614
    Try:
    SELECT tbMaster.Phones, IIf(InStr(1,[Phones],":")-1=-1,[Phones],Left([phones],InStr(1,[Phones],":")-1)) AS FirstPhone, IIf(InStr(1,Phones],":")-1=-1,"",Right([Phones],Len([phones])-InStrRev([Phones],":",-1)-1)) AS SecondPhone FROM tbMaster;
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  7. #7
    JamesBond is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Apr 2014
    Posts
    11
    Bob that works perfectly now thank you for all your help much appreciated

  8. #8
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows XP Access 2003
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,614
    Quote Originally Posted by JamesBond View Post
    Bob that works perfectly now thank you for all your help much appreciated
    You're welcome. Glad to be of help
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Can access take one text string and split it into two?
    By hobsondm01 in forum Database Design
    Replies: 2
    Last Post: 06-09-2011, 09:09 AM
  2. Replies: 15
    Last Post: 01-12-2011, 05:13 PM
  3. Replies: 1
    Last Post: 12-01-2010, 11:01 AM
  4. Replies: 2
    Last Post: 07-26-2010, 11:28 AM
  5. Split a string for use as query parameters.
    By Hobbes29 in forum Forms
    Replies: 3
    Last Post: 06-27-2010, 04:39 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums