first 3 digits

1. Senior Member
Join Date
Dec 1969
Posts
666

## first 3 digits

I want to select all the users from the databse that their area code is the first 3 digits of their phone number.<BR><BR>This is wht I got so far:<BR>SQL = "name,email FROM usersTBL WHERE phone_number=&#039;" & areaCode & "&#039;"<BR><BR>I need to know how can I select only the first 3 digits of the phone_number when I&#039;m runing this SQL.<BR><BR>Thanks!

2. Senior Member
Join Date
Dec 1969
Posts
935

## RE: first 3 digits

well, maybe there&#039;s a better way but you can do this:<BR>let&#039;s say the length of the phone number is 9 digits and the area code length is 3 digits.<BR>do this:<BR>SQL = "name,email FROM usersTBL WHERE phone_number BETWEEN &#039;" & areaCode & "000000&#039; AND &#039;" & areaCode & "999999&#039;"<BR><BR>this should do the job i guess.

3. Senior Member
Join Date
Dec 1969
Posts
1,846

## RE: first 3 digits

SQL = "name,email FROM usersTBL WHERE Left(phone_number,3)=&#039;" & areaCode & "&#039;" <BR><BR>You may also want to do a LTrim in case there&#039;s any leading spaces:<BR><BR>SQL = "name,email FROM usersTBL WHERE Left(LTrim(phone_number),3)=&#039;" & areaCode & "&#039;"

4. Senior Member
Join Date
Dec 1969
Posts
666

## RE: first 3 digits

Thanks guys!

5. Senior Member
Join Date
Dec 1969
Posts
96,118

## This is a better soluton IF...

...the phone_number field is NUMERIC in the database.<BR><BR>Then it&#039;s great.<BR><BR>SQL = "name,email FROM usersTBL " _<BR> & " WHERE phone_number BETWEEN " & (areacode & "0000000") & " AND " & (areacode & "9999999")<BR><BR>See it?<BR><BR>If you give it area code of 714, for example, that becomes<BR> WHERE phone_number BETWEEN 7140000000 AND 7149999999<BR><BR>I guess that also works even if phone_number is a text field, so long as there are no spaces, etc., in the stored values of phone_number.<BR><BR>Another way to do it:<BR><BR>SQL = "name,email FROM usersTBL " _<BR> & " WHERE phone_number LIKE &#039;" & areacode & "%&#039;"<BR><BR>Again, this should be used for non-numeric phone_number fields.<BR><BR>

#### Posting Permissions

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