I understand that WHERE 1 = '1' returns true, and also WHERE 222 = CONVERT(varchar, 222) will return true in T-SQL.
But, when working with IN, can the test_expression and the subquery be of different datatypes? For example can the test_expression be an int, and the subquery varchar?
They can be: all datatypes will be cast to the highest based on “datatype precedence” rules.
Logically,
x in (a,b,c)is actuallyx=a or x=b or x=cof course. This is relevant for this contrived case using scalar values. I really don’t know if all values are CAST to float (the highest here) before comparison (..IN..) or cast per comparison (..OR..OR..)For a subquery in the IN clause, then the column has a data type that will be CAST as per the scalar rules
Edit, after comment:
All datatypes will be implicitly CAST
or