Hey all i have the following query:
SELECT REPLACE(REPLACE(REPLACE(REPLACE(tmpTable.Caller_Number,'-',''),'(',''),')',''),' ','')
FROM tmpTable
WHERE EXISTS
(SELECT REPLACE(REPLACE(REPLACE(REPLACE(OnlineAppDetails.addPhone,'-',''),'(',''),')',''),' ','')
FROM OnlineAppDetails
WHERE OnlineAppDetails.addPhone = tmpTable.Caller_Number)
ORDER BY GroupsBy ASC;
It returns no records… However, when i do this query:
SELECT *
FROM tmpTable
WHERE EXISTS
(SELECT * FROM OnlineAppDetails
WHERE SUBSTRING(OnlineAppDetails.addPhone,2,3)+'-'+SUBSTRING(OnlineAppDetails.addPhone,7,4)+SUBSTRING(OnlineAppDetails.addPhone,11,4) = tmpTable.Caller_Number)
ORDER BY GroupsBy ASC;
It returns records..
The phome number is formatted like:
for OnlineAppDetails: (xxx) xxx-xxxx
for tmpTable: xxx-xxx-xxxx
All i am doing is taking out the – ( ) and [space] in the phone number just to be sure i am being returned all the records i should be. I know there are matches in both tables so taking out – ( ) and [space] should still result in records returned.
update
Duh…. knew it was going to be simple.
SELECT *
FROM tmpTable
WHERE EXISTS
(SELECT *
FROM OnlineAppDetails
WHERE REPLACE(REPLACE(REPLACE(REPLACE(OnlineAppDetails.addPhone,'-',''),'(',''),')',''),' ','') = REPLACE(REPLACE(REPLACE(REPLACE(tmpTable.Caller_Number,'-',''),'(',''),')',''),' ',''))
ORDER BY GroupsBy ASC;
For sure the first query wouldn’t return any rows:
that is because, the
WHERE OnlineAppDetails.addPhone = tmpTable.Caller_Numberpart of the inner query will yield zero rows (given the expect format of the respective fields), hence the EXISTS will always be false.Note BTW that the string manipulation done in the SELECT portion on this (indeed of any) EXISTS subquery, cannot have any effect on the result of the EXISTS predicate; that is why the idiom used is typically:
... WHERE EXISTS * FROM ....The second query query works because the string transformation is applied in the WHERE clause. This transformation makes the OnlineAppDetails.addPhone value have the same format as the format of the tmpTable.Caller_Number, hence providing a opportunity to find matches.