If I have a table with columns like so:
KeyName varchar(50)
RowNumber int
LocationValue sql_variant
select * from myTable where LocationValue = 'some string' -- no results, but no error
select * from myTable where CAST(LocationValue AS VARCHAR) = 'some string' --works
If LocationValue can contain either numeric or string data, is it mandatory to CAST that column to NUMERIC or VARCHAR in order to apply criteria to it?
Not doing so doesn’t cause any error, however no results are returned.
It isn’t mandatory as such, but you need to CAST it to a meaningful type for that comparison: if only for maintainability and your sanity.
The rules for conversions are here on MSDN. Above that, you have things on collation and SET ANSI_PADDING that will all affect results. An explicit CAST overrides these rules.
I can’t actually say why you see this because I haven’t used sql_variant for years but I refer you to my first statement about “sanity”