Less than or Equal to
operator compares two expressions. When you compare two nonnull expression and
left hand operand is less than or equal to right hand operator then the result
is TRUE and right hand operand is less than or equal to left hand operand then
result is FALSE. When you set ANSI_NULLS ON and any of the
expressions are null then the result is null. When you set ANSI_NULLS OFF
and any of the expressions are null then the result set is FALSE. But if both
expressions are null then the result set is TRUE.
Syntax of <= ( Less Than or Equal To ) Comparison Operator :
expression <=
expression
Where expression is any
valid sql expression. Both expression must have implicitly convertible data
types. Less than or Equal to operator returns boolean value TRUE or
FALSE.
Examples of <= ( Less Than or Equal To ) Comparison Operator :
Example 1 : Using <= operator in where clause
SELECT
ProductName,UnitPrice
FROM products
WHERE unitprice <=
18.00
Output
ProductName UnitPrice
Chai
18.00
Aniseed Syrup 10.00
Konbu 6.00
Genen Shouyu 15.50
Pavlova 17.45
Teatime Chocolate
Biscuits 9.20
Above query displays
rows having UnitPrice less than or equal to $18.
Example 2 : Use of
less than or equal to operator to compare values of table fields
SELECT ProfitPerProduct
=
CASE
WHEN SoldPrice <= ActualPrice THEN '$' + CONVERT(VARCHAR, (ActualPrice - SoldPrice)) + ' loss on ' + ProductName
ELSE '$' +
CONVERT(VARCHAR, (SoldPrice - ActualPrice)) + ' profit on ' +
ProductName
END
FROM Products
Output
ProfitPerProduct
$21 profit on Chai
$2 loss on Chang
$3 profit on Aniseed
Syrup
$31 profit on Chef
Anton's Cajun Seasoning
$21.35 loss on Chef
Anton's Gumbo Mix
Above example describes
use of less than or equal to operator to compare SoldPrice and ActualPrice
field values of table.
Example 3 : Use of less
than or equal to operator in select query
DECLARE @Unit1 INT,
@Unit2 INT
SET @Unit1 = 20
SET @Unit2 = 20
SELECT Message =
CASE
WHEN @Unit1 <= @Unit2 THEN 'Unit1 is less than or equal to Unit2.'
ELSE
'Unit1 is greater than Unit2.'
END
Output
Message
Unit1 is less than or
equal to Unit2.
Above example describes
use of less than or equal to comparison operator in sql query. We can use less
than or equal to operator in select query to compare variable or fields of
table.
Example 4 : Comparing
value of two variables
DECLARE @Unit1 INT,
@Unit2 int
SET @Unit1 = 10
SET @Unit2 = 25
IF(@Unit1 <= @Unit2)
BEGIN
PRINT 'Unit1 is less than or equal to Unit2.'
END
ELSE
BEGIN
PRINT 'Unit1 is greater than Unit2.'
END
Output
Unit1 is less than or
equal to Unit2.
Above example explains
use of less than or equal to comparison operator to compare two
variables.