I've just come across this in a WHERE clause:
AND NOT (t.id = @id)
How does this compare with:
AND t.id != @id
Or with:
AND t.id <> @id
I'd always write the latter myself, but clearly someone else thinks differently. Is one going to perform any better than the other? I know that using <>
or !=
is going to bust any hopes for using an index that I might have had, but surely the first approach above will suffer the same problem?
In SQL, you can use the = operator to test for equality in a query. In this example, the SELECT statement above would return all rows from the suppliers table where the supplier_name is equal to Microsoft.
The sql equal operator is used to check whether two expressions are equal or not. If it's equal, the condition will be true and will return matched records. The sql not equal operator is used to check whether two expressions are equal or not.
>= (Greater Than or Equal To) (Transact-SQL) - SQL Server | Microsoft Docs.
These 3 will get the same exact execution plan
declare @id varchar(40) select @id = '172-32-1176' select * from authors where au_id <> @id select * from authors where au_id != @id select * from authors where not (au_id = @id)
It will also depend on the selectivity of the index itself of course. I always use au_id <> @id myself
Note that the != operator is not standard SQL. If you want your code to be portable (that is, if you care), use <> instead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With