Why do these queries return different values? The first returns a result set as expected, but the second (which as far as I can tell is exactly the same) does not. Any thoughts?
1:
declare @c varchar(200)
set @c = 'columnName'
select top 1 *
from myTable
where @c is not null
and len(convert(varchar, @c)) > 0
2:
SELECT top 1 *
FROM myTable
WHERE columnName IS NOT NULL
and len(convert(varchar,columnName)) > 0
It’s because they aren’t the same query — your variable text does not get inlined into the query.
In query 1 you are validating that
@cis not null (true, you set it) and that its length is greater than 0 (true, it’s 10). Since both are true, query 1 becomes:(It will return the first row in myTable based on an appropriate index.)
EDIT: Addressing the comments on the question.
Now when I run this both queries return the same result. You’ll have to tell me where I’m misrepresenting your actual data / query to get more help (or just expand upon this to find a solution).