SELECT Annuities.ID
,Annuities.APolicyNumber
,CancellationModes.ID
,InStr([Annuities].[APolicyNumber], [CancellationModes].[CPolicyNumber])
,CancellationModes.CPolicyNumber
FROM Annuities
,CancellationModes
WHERE (((InStr([Annuities].[APolicyNumber], [CancellationModes].[CPolicyNumber])) > 0));
SELECT TBLCancellationModes.*, AnnutiesTBL.ID
FROM TBLCancellationModes INNER JOIN AnnutiesTBL ON AnnutiesTBL.PolicyNumber like TBLCancellationModes.PolicyNumber & "*";
...now there is little change in the requirement. Now I need to match each policynumber of TBLCancellationModes
with policynumbers in Annuties but just in the beginning of the string.
SELECT Annuities.ID
,Annuities.APolicyNumber
,CancellationModes.ID
,InStr([Annuities].[APolicyNumber], [CancellationModes].[CPolicyNumber])
,CancellationModes.CPolicyNumber
FROM Annuities
,CancellationModes
WHERE (((InStr([Annuities].[APolicyNumber], [CancellationModes].[CPolicyNumber]))[COLOR="Red"] = 1[/COLOR]))
How big are both tables? And are those fields indexed?
Good point jd. I think it depends on usage. If none of the wildchard characters are used then it may use indexing (which defeats the whole point of doing a pattern match). But the point of using LIKE in this case was to take advantage of the INNER JOIN which, naturally, performs better than a WHERE clause.However, if this is all a learning exercise, then you are experiencing some of the issues associated with database table and field design. I don't think that the LIKE operator can take advantage of indexing, so use of LIKE is not likely to lessen your query execution time.
SELECT TBLCancellationModes.*, AnnutiesTBL.ID
FROM TBLCancellationModes INNER JOIN AnnutiesTBL ON TBLCancellationModes.PolicyNumber = Left(AnnutiesTBL.PolicyNumber, Len(TBLCancellationModes.PolicyNumber));