Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 36875
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T14:27:43+00:00 2026-05-10T14:27:43+00:00

I have a table which is full of arbitrarily formatted phone numbers, like this

  • 0

I have a table which is full of arbitrarily formatted phone numbers, like this

027 123 5644 021 393-5593 (07) 123 456 042123456 

I need to search for a phone number in a similarly arbitrary format ( e.g. 07123456 should find the entry (07) 123 456

The way I’d do this in a normal programming language is to strip all the non-digit characters out of the ‘needle’, then go through each number in the haystack, strip all non-digit characters out of it, then compare against the needle, eg (in ruby)

digits_only = lambda{ |n| n.gsub /[^\d]/, '' }  needle = digits_only[input_phone_number] haystack.map(&digits_only).include?(needle) 

The catch is, I need to do this in MySQL. It has a host of string functions, none of which really seem to do what I want.

Currently I can think of 2 ‘solutions’

  • Hack together a franken-query of CONCAT and SUBSTR
  • Insert a % between every character of the needle ( so it’s like this: %0%7%1%2%3%4%5%6% )

However, neither of these seem like particularly elegant solutions.
Hopefully someone can help or I might be forced to use the %%%%%% solution

Update: This is operating over a relatively fixed set of data, with maybe a few hundred rows. I just didn’t want to do something ridiculously bad that future programmers would cry over.

If the dataset grows I’ll take the ‘phoneStripped’ approach. Thanks for all the feedback!


could you use a "replace" function to strip out any instances of "(", "-" and " ",

I’m not concerned about the result being numeric. The main characters I need to consider are +, -, (, ) and space So would that solution look like this?

SELECT * FROM people  WHERE  REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(phonenumber, '('),')'),'-'),' '),'+') LIKE '123456' 

Wouldn’t that be terribly slow?

  • 1 1 Answer
  • 3 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T14:27:43+00:00Added an answer on May 10, 2026 at 2:27 pm

    This looks like a problem from the start. Any kind of searching you do will require a table scan and we all know that’s bad.

    How about adding a column with a hash of the current phone numbers after stripping out all formatting characters. Then you can at least index the hash values and avoid a full blown table scan.

    Or is the amount of data small and not expected to grow much? Then maybe just sucking all the numbers into the client and running a search there.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table which looks like this. +----+--------+---------+-----------+------------------------+ | id | parent |
I have a database table which is full-text indexed and i use the CONTAINS-function
I have a table full of id's (tID) which are created with a w
I have a table which stores the data related to posts in this format
Is this any possible? I'd like to have something like User.avatar.to_url which would then
I have a table view which is autosizing to the full frame of the
I have a legacy mysql database and there's this table which has a few
i have a table with a column named CustomerName which stores a customer's full
I have a Questions table which looks like that as you see, there are
I have a database with a table which is full of conditions and error

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.