<> (Not Equal To) (Transact-SQL)


THIS TOPIC APPLIES TO: yesSQL Server (starting with 2008)yesAzure SQL DatabaseyesAzure SQL Data Warehouse yesParallel Data Warehouse

Compares two expressions (a comparison operator). When you compare nonnull expressions, the result is TRUE if the left operand is not equal to the right operand; otherwise, the result is FALSE. If either or both operands are NULL, see the topic SET ANSI_NULLS (Transact-SQL).

Topic link icon Transact-SQL Syntax Conventions

-- Syntax for SQL Server, Azure SQL Database, Azure SQL Data Warehouse, Parallel Data Warehouse  
expression <> expression  

Is any valid expression. Both expressions must have implicitly convertible data types. The conversion depends on the rules of data type precedence.


A. Using <> in a simple query

The following example returns all rows in the Production.ProductCategory table that do not have value in ProductCategoryID that is equal to the value 3 or the value 2.

-- Uses AdventureWorks  
SELECT ProductCategoryID, Name  
FROM Production.ProductCategory  
WHERE ProductCategoryID <> 3 AND ProductCategoryID <> 2;  

Here is the result set.

ProductCategoryID Name  
----------------- --------------------------------------------------  
1                 Bikes  
4                 Accessories  
(2 row(s) affected)  

Data Types (Transact-SQL)
Operators (Transact-SQL)
Comparison Operators (Transact-SQL)

Community Additions