You want to make sure that at least one of the tables uses its actual data type for identifiers and that it can use index search, if possible. It depends on the selectivity of your request and the speed of matches, but to determine which one needs to be converted to another. If you know that you need to scan the entire first table, then you still cannot use the search, and you must convert this identifier to the data type of another table.
To make sure you can use indexes, also avoid LIKE. For example, it is much better to have:
WHERE T1.ID = CAST(T2.ID AS VARCHAR) OR T1.ID = RIGHT('0000000000' + CAST(T2.ID AS VARCHAR), 10)
than:
WHERE T1.ID LIKE '%' + CAST(T2.ID AS VARCHAR)
As Stephen Low noted, the second request may also be inaccurate.
If you are going to use all the lines from T1, though (in other words, LEFT OUTER JOIN to T2), you might be better off:
WHERE CAST(T1.ID AS INT) = T2.ID
Make some query plans with each method if you are not sure and see what works best.
The absolute best route to go, like others, has suggested and changed the data type of the tables so that they match, if at all possible. Even if you cannot do this before this project is completed, put it on your "to do" list in the near future.
Tom h
source share