2

I have this table, named Prospects :

+---------+-------------+
| Name    | PhoneNumber |
+---------+-------------+
|John     | 08199999    |
|Viona    | 08332222    |
+---------+-------------+

on the other hand I have this table too, named SMSD :

+-----------------+---------------+
| Message         | PhoneNumber   |
+-----------------+---------------+
|Hello World!     | +628199999    |
|Hi World         | +628332222    |
+-----------------+---------------+

now I need to do LEFT JOIN to that both tables based on PhoneNumber : Prospects.PhoneNumber = SMSD.PhoneNumber while on the SMSD table phone number always had country code prefix.

thanks!

2
  • are the length of the values fixed? eg +628199999 and 08199999 and also the pattern? Commented Feb 1, 2013 at 8:31
  • the only thing you can use as the pattern is the prefix. always started with +62 on SMSD table. the length of number are dynamic. for some GSM operator has 12 digits number, but other might have 11 or even 10 digits number. Commented Feb 1, 2013 at 8:33

2 Answers 2

2
select * from Prospects
left join SMSD on substring(SMSD.PhoneNumber, 4) = substring(Prospects.PhoneNumber, 2)

(Not tested)

This should remove the country code prefix from the SMSD phone number and the leading zero from the Prospects phone number before performing the join.

Sign up to request clarification or add additional context in comments.

Comments

0
SELECT *
FROM prospects AS t1
JOIN SMSD AS t2
ON 
CONCAT(@prefix, SUBSTRING(t1.PhoneNumber, 2)) = t2.PhoneNumber
WHERE t1.PhoneNumber LIKE @prefix%

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.