Click to Rate and Give Feedback
MSDN
MSDN Library
SQL Server
SQL Server 2008
Database Engine
Technical Reference
 Comparison Operators (Transact-SQL)
Community Content
In this section
Statistics Annotations (0)
Collapse All/Expand All Collapse All
Other versions are also available for the following:
SQL Server 2008 Books Online (October 2009)
Comparison Operators (Transact-SQL)

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 Transact-SQL 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)

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 SQL Server 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 an equals (=) operator returns TRUE if both expressions are NULL. For example, NULL = NULL returns TRUE when SET ANSI_NULLS is OFF.

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:

USE AdventureWorks;
GO
DECLARE @MyProduct int;
SET @MyProduct = 750;
IF (@MyProduct <> 0)
   SELECT ProductID, Name, ProductNumber
   FROM Production.Product
   WHERE ProductID = @MyProduct
GO
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement | Site Feedback
Page view tracker