Home > T-SQL > Comparison operators (=, >, >=, <, <=, , !, !>, !<) – SQL SERVER – Transact-SQL

Comparison operators (=, >, >=, <, <=, , !, !>, !<) – SQL SERVER – Transact-SQL


Comparison symbols are used to compare two values. For instance, it is to check whether a numeric value 5 is greater than numeric value 6 or not. Comparison symbols can be seen within the if statement and in SQL where clause.

The output of any comparison statement returns a boolean value True (1) or False (0).

Comparison operators

= Equal to

> Greater than

< Less than

<> Not Equal to

>= Greater than or Equal to

<= Less than or Equal to

! (Comparison Not operator) we can use this operator with the basic comparison operators.

!= Not Equal to

!> Not greater than

!< Not less than

Example:

Declare @ValueA as integer = 5,
		@valueB as integer = 7

--Equal to operator
if @ValueA = @valueB
 print 'True'
else 
 Print 'False'

--Greater than operator
if @ValueA > @valueB
 print 'True'
else 
 Print 'False'

 --Less than operator
if @ValueA < @valueB
 print 'True'
else 
 Print 'False'

 --Not Equal to operator
if @ValueA <> @valueB
 print 'True'
else 
 Print 'False'

 --Greater than or Equal to operator
if @ValueA >= @valueB
 print 'True'
else 
 Print 'False'

 --Less than or Equal to operator
if @ValueA <= @valueB
 print 'True'
else 
 Print 'False'

 --Not Equal to operator
if @ValueA != @valueB
 print 'True'
else 
 Print 'False'

 --Not greater than  operator
if @ValueA !> @valueB
 print 'True'
else 
 Print 'False'

 --Not less than operator
if @ValueA !< @valueB
 print 'True'
else 
 Print 'False'

We cannot use this operation with any other combination like !>=( Not greater than or equal to). In this case, we have split  this comparison statement in to two blocks with logical operator OR like this (@ValueA !> @ValueB) OR (@ValueA = @ValueB).

Thanks for reading.

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: