Equals operator compares two expressions. When you compare two nonnull expression and operands are equal then result is TRUE and operands are not equal 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 = ( Equals ) Comparison Operator :
expression = expression
Where expression is any valid sql expression. Both expression must have implicitly convertible data types. Equals operator returns boolean value TRUE or FALSE.
Examples of = ( Equals ) Comparison Operator :
Example 1 : Use of equals operator in select query.
DECLARE @ActualPrice INT, @SoldPrice int
SET @ActualPrice = 20
SET @SoldPrice = 10
SELECT Message =
CASE
WHEN @ActualPrice = @SoldPrice THEN 'Actual price and sold price are same.'
ELSE 'Actual price and sold price are different.'
END
Output
Message
Actual price and sold price are different.
Above example describes use of equals operator in sql query. We can use equal operator in select query to compare variable or fields of table.
Example 2 : Comparing value of two variables.
DECLARE @ActualPrice INT, @SoldPrice int
SET @ActualPrice = 10
SET @SoldPrice = 10
IF(@ActualPrice = @SoldPrice)
BEGIN
PRINT 'Actual price and sold price are same.'
END
ELSE
BEGIN
PRINT 'Actual price and sold price are different.'
END
Output
Actual price and sold price are same.
Above example explains use of equals comparison operator to compare two variables.