This is because they are not the same query - your variable text does not fall into the query.
In request 1, you confirm that @c not null (true, you set it) and its length is greater than 0 (true, it 10). Since both are true, query 1 becomes:
select top 1 * from myTable
(It will return the first row in myTable based on the corresponding index.)
EDIT: Refer to comments on a question.
declare @myTable table ( columnName varchar(50) ) insert into @myTable values ('8') declare @c nvarchar(50) set @c = 'columnName' select top 1 * from @myTable where @c is not null and len(convert(varchar, @c)) > 0 select top 1 * from @myTable where columnName is not null and len(convert(varchar,columnName)) > 0
Now when I run this, both queries return the same result. You must tell me where I am distorting your actual data / request in order to get additional help (or just expand it to find a solution).
source share