Skip to content

Latest commit

 

History

History
65 lines (54 loc) · 3.56 KB

comparison-operators-transact-sql.md

File metadata and controls

65 lines (54 loc) · 3.56 KB
title description author ms.author ms.date ms.service ms.subservice ms.topic ms.custom helpviewer_keywords dev_langs monikerRange
Comparison Operators (Transact-SQL)
Comparison Operators (Transact-SQL)
rwestMSFT
randolphwest
03/15/2017
sql
t-sql
reference
ignite-2024
expressions [SQL Server], testing
operators [Transact-SQL], comparison
testing expressions
Boolean data type
Boolean expressions
comparing expressions
comparison operators [SQL Server]
TSQL
=azuresqldb-current || >=sql-server-2016 || >=sql-server-linux-2017 || =azuresqldb-mi-current || =fabric

Comparison Operators (Transact-SQL)

[!INCLUDE SQL Server Azure SQL Database Azure SQL Managed Instance-fabricsqldb]

Comparison operators test whether two expressions are the same. Comparison operators can be used on all expressions except expressions of the text, ntext, or image data types. The following table lists the [!INCLUDEtsql] comparison operators.

Operator Meaning
= (Equals) Equal to
> (Greater Than) Greater than
< (Less Than) Less than
>= (Greater Than or Equal To) Greater than or equal to
<= (Less Than or Equal To) Less than or equal to
<> (Not Equal To) Not equal to
!= (Not Equal To) Not equal to (not ISO standard)
!< (Not Less Than) Not less than (not ISO standard)
!> (Not Greater Than) Not greater than (not ISO standard)

Boolean Data Type

The result of a comparison operator has the Boolean data type. This has three values: TRUE, FALSE, and UNKNOWN. Expressions that return a Boolean data type are known as Boolean expressions.

Unlike other [!INCLUDEssNoVersion] data types, a Boolean data type cannot be specified as the data type of a table column or variable, and cannot be returned in a result set.

When SET ANSI_NULLS is ON, an operator that has one or two NULL expressions returns UNKNOWN. When SET ANSI_NULLS is OFF, the same rules apply, except for the equals (=) and not equals (<>) operators. When SET ANSI_NULLS is OFF, these operators treat NULL as a known value, equivalent to any other NULL, and only return TRUE or FALSE (never UNKNOWN).

Expressions with Boolean data types are used in the WHERE clause to filter the rows that qualify for the search conditions and in control-of-flow language statements such as IF and WHILE, for example:

-- Uses AdventureWorks  
  
DECLARE @MyProduct INT;  
SET @MyProduct = 750;  
IF (@MyProduct <> 0)  
   SELECT ProductID, Name, ProductNumber  
   FROM Production.Product  
   WHERE ProductID = @MyProduct;  

See Also

Expressions (Transact-SQL)
Operators (Transact-SQL)