Reputation: 44352
@myvar is an nvarchar(30). How can I do the following evaluation without using an OR?
@myvar = '' OR (table1.col1 like @myvar)
Is there some other way that is more efficient?
Upvotes: 0
Views: 62
Reputation: 15852
If one had an interview question where OR
was forbidden and De Morgan's laws were forgotten then one might use:
case
when @myvar = '' then 42
when table1.col1 like @myvar then 666
else 0 end > 0
One might also experiment with Len( @myvar ) = 0
.
Upvotes: 1
Reputation: 107806
There are other ways, in fact there are many. However, the form you have performs the best for most versions of SQL Server 2005 onwards if you add OPTION (RECOMPILE).
Look here for the definitive guide to searching with dynamic conditions. Follow the link for your version of SQL Server.
Upvotes: 1