MS SQL Server Concepts and Programming Question:
What Are Logical/Boolean Operations in MS SQL Server?
Answer:
Logical (Boolean) operations are performed on Boolean values with logical operators like 'AND', 'OR', or 'NOT'. Logical operations return Boolean values. SQL Server 2005 supports the following logical operations:
* AND - Returns TRUE if both operands are TRUE.
* OR - Returns TRUE if one of the operands is TRUE.
* NOT - Returns TRUE if the only operand is FALSE.
Logical operations are commonly used to combine Boolean values resulted from comparison operations. The following tutorial exercise shows you a good example:
DECLARE @income MONEY;
DECLARE @marriage_status VARCHAR(10);
SET @income = 55000.00;
SET @marriage_status = 'Single';
SELECT CASE WHEN
(@marriage_status = 'Married' AND @income < 65000)
OR (@marriage_status = 'Single' AND @income < 35000)
THEN 'Qualified for the benefit.'
ELSE 'Not qualified for the benefit.'
END;
GO
Not qualified for the benefit.
* AND - Returns TRUE if both operands are TRUE.
* OR - Returns TRUE if one of the operands is TRUE.
* NOT - Returns TRUE if the only operand is FALSE.
Logical operations are commonly used to combine Boolean values resulted from comparison operations. The following tutorial exercise shows you a good example:
DECLARE @income MONEY;
DECLARE @marriage_status VARCHAR(10);
SET @income = 55000.00;
SET @marriage_status = 'Single';
SELECT CASE WHEN
(@marriage_status = 'Married' AND @income < 65000)
OR (@marriage_status = 'Single' AND @income < 35000)
THEN 'Qualified for the benefit.'
ELSE 'Not qualified for the benefit.'
END;
GO
Not qualified for the benefit.
Previous Question | Next Question |
How To Test Values Returned by a Subquery with the IN Operator? | How To Use "IF ... ELSE IF ... ELSE ..." Statement Structures in MS SQL Server? |