1

I have a SQL statement which - trimmed down for this question - looks like this:

SELECT * 
FROM Customers
LEFT JOIN SMS_Blacklist ON Customers.MobileNumber = SMS_Blacklist.Mobile_Number
WHERE SMS_Blacklist.Mobile_Number IS NULL

This works for the most part however the database has no data continuity or consistency and when the data looks like this:

  • Customer.MobileNumber = "07123 456789",
  • SMS_Blacklist.Mobile_Number = "07123456789"

it returns the record when it shouldn't (because the phone numbers don't match).

My question is, is it possible to perform a string function (i.e. string.replace(" ", "")) in the middle of the SQL statement so it might look something like this (I think probably not):

...
ON (Customers.MobileNumber).replace(" ","") = SMS_Blacklist.Mobile_Number
...

or how could I achieve something like this?

I am creating the SQL statement using new SqlCommand() in C#.

1 Answer 1

3

Rectifying phone numbers is very tricky. But if you just want to remove spaces and hyphens you can do replace() in the SQL:

SELECT c.
FROM Customers c LEFT JOIN
     SMS_Blacklist b
     ON b.Mobile_Number = REPLACE(REPLACE(c.MobileNumber, ' ', ''), '-', '')
WHERE b.Mobile_Number IS NULL;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.