|
-
Nov 26th, 2007, 10:32 AM
#1
Thread Starter
Junior Member
[RESOLVED] Help with string functions
Does anyone know of a quick fix to this problem?
I have a column in my table that contains various phone numbers. However to keep these unique any phone number that is a duplicate has been assigned a ~ and the number referring to the duplicate occurrence. So I am left with data as shown below
01234567890
01234567890~1
01234567890~2
01345678901
01345678901~1
01345678901~2
01345678901~3
etc....
What I want to do is ignore the ~ and the number on the end of the phone number like so:
Left$(PhoneNumber, 11)
The only problem being that some of my phone numbers are 10 digits and some are 11 digits. Is there any function that will look at a string and get everything to the left of it i.e. everything left of the character "~"
Left$(PhoneNumber, "~")
Many Thanks
Daniel Stead
-
Nov 26th, 2007, 10:37 AM
#2
Re: Help with string functions
What are you using for a front end?
VB6 and VB.NET could parse that using the Split Function.
-
Nov 26th, 2007, 10:50 AM
#3
Re: Help with string functions
And if you want to do it in an SQL statement (which would be more complicated), which database system are you using?
-
Nov 26th, 2007, 11:20 AM
#4
Thread Starter
Junior Member
Re: Help with string functions
Im using micorsoft access 2003 with vb version 6.
Ideally i would like to do this using sql but would welcome either option as i have tired many different options with no success.
-
Nov 26th, 2007, 11:36 AM
#5
Re: Help with string functions
With VB6
Code:
Dim strPhoneNo() As String
strPhoneNo = "01234567890~2"
strPhoneNo = Split(strPhoneNo, "~")
Msgbox strPhoneNo(0)
-
Nov 26th, 2007, 11:40 AM
#6
Frenzied Member
Re: Help with string functions
Tengo mas preguntas que contestas
-
Nov 27th, 2007, 06:09 AM
#7
Thread Starter
Junior Member
Re: Help with string functions
Thanks for the solution but does anyone know how i can do this in sql or if it is possible in sql? It would just make my life alot easier.
-
Nov 27th, 2007, 06:23 AM
#8
Frenzied Member
Re: Help with string functions
You could use strPhone(0) in SQL. I was thinking above of using Replace() to get rid of the ~, but realized you'd still have the extra digit. I suppose you could combine Left() & Replace() in SQL.
Why do some phone numbers have 11 digits?
Tengo mas preguntas que contestas
-
Nov 27th, 2007, 11:54 AM
#9
Re: Help with string functions
Based on a little test, I think this should work:
Code:
SELECT PhoneNumber, IIf(Instr(PhoneNumber,"~")=0,PhoneNumber,Left(PhoneNumber,Instr(PhoneNumber,"~")-1))
FROM tablename;
However, I can't help feeling that having the data like that in the first place is a really bad idea - it could easily have been avoided by using an Autonumber field.
edit: corrected typo (for some reason had written "s" instead of "~" )
Last edited by si_the_geek; Nov 29th, 2007 at 09:57 AM.
-
Nov 29th, 2007, 06:08 AM
#10
Thread Starter
Junior Member
Re: Help with string functions
Thanks for the reply and i totally agree that it is a bad way to store data but i can only work with what i am given.
I can see how the code would work that you have supplied as if the string has a ~ it finds the poistion of this and uses this to get everything -1 of that postion.
I am sturggling however to work out the first part of the code i.e.
Instr(PhoneNumber, "s") = 0
What does this do as my instinct would be search for the value s in the string?
Many thanks for your help
-
Nov 29th, 2007, 06:26 AM
#11
Thread Starter
Junior Member
Re: Help with string functions
Never mind i figured it out and it works perfectly thanks for your help
Dan Stead
-
Nov 29th, 2007, 08:58 AM
#12
Re: Help with string functions
Just for clarification, Instr does search for a string within another - it returns the position that it is found at, or 0 if it is not found.
As you now have it sorted out, could you please do us a little favour, and mark the thread as Resolved?
(this saves time reading for those of us who like to answer questions, and also helps those who search to find answers)
You can do this by clicking on "Thread tools" just above the first post in this thread, then "Mark thread resolved".
-
Nov 29th, 2007, 09:24 AM
#13
Re: Help with string functions
I know that it's there because it will always return true, therefore executing the command that you really want, but I don't understand why it's necessary to construct a self-fulfilling IIf statement. Can you explain si?
-
Nov 29th, 2007, 09:56 AM
#14
Re: Help with string functions
It was actually a typo, instead of "s" it should have been "~" (based on his latest post, I'm assuming Dan noticed and corrected it!)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|