Currently I have a query which is partly based on a join on two tables according to two number columns within them.
Say one table has a number like 123456789999 (NUM1)
And the other table has a number ranging from 1 – 9999 (NUM2)
I want to pull out the records which have ‘NUM2’ within the 5th – 8th digits of ‘NUM1’
Currently I am doing something like this,
FROM Table1 AS T INNER JOIN Table2 AS S
ON SUBSTRING(T.num1, 5, 4) = S.num2
I know it should be retrieving approx 100 records, but I only get 8. I believe it to be because of the small ranges within number two. Where have I gone wrong? OR how could my code be made more robust/effective?
You need to use
CASTlike this:SEE THIS FIDDLE
For more info see SQL SERVER – Convert Text to Numbers (Integer) – CAST and CONVERT