google-sheetsstripreformat

QUERY ignores phone numbers typed in different format


I have a test page here: https://docs.google.com/spreadsheets/d/1q4qRgXSq6xXhddArPCoBIuOOupRvokUd0andADCNN0Q/edit?usp=sharing

In a public "Sheets" document people add their own phone number, but different people are typing their number in different ways, such as:

23423423444
234-234-2344
234.234.2344
(234) 234-2344

When I perform QUERY actions on this data, it cannot understand the different data formats. It can only pull one data type, so it ignores the other data types. For example, in the data shown above it interprets the first row as a number, so it regards Row 2 and Row 4 as null because it thinks they are strings of text.

I am trying to come up with an intermediate formula that will take all such numbers, strip them of everything but their numbers, and then impose a common format, such as xxx-xxx-xxxx. I would then use this column for my QUERY needs. Unfortunately, I don't know enough code to accomplish this. Can anyone help?


Solution

  • This sounds like a XY Problem, you don't want QUERY to ignore the different data-type and the issue you are asking help with is to format the numbers as ###-###-#### so QUERY doesn't ignore it. However, that's not the easiest way to solve this problem; an easy fix is to either change the format of the column to Plain text or do it from the formula, either by using the TO_TEXT function or by simply appending an empty string &"" to the data:

    =ARRAYFORMULA(QUERY({B5:B,C5:C}&"","select * where Col1 is not null",0))
    

    enter image description here

    Since you are using the QUERY function for filtering, you could also solve the same problem using the FILTER function, which doesn't have this limitation.

    If you actually want to format the numbers as ###-###-####, you can use:

    =ARRAYFORMULA(QUERY({B5:B,REGEXREPLACE(REGEXREPLACE(C5:C&"","\D",),
        "(\d{3})(\d{3})(\d{4})","$1-$2-$3")},"where Col1 is not null"))
    

    enter image description here