SQL Server Boolean Type: Three Values and Their Effect on WHERE, IF, and WHILE
SQL Server comparison operators return a Boolean type with TRUE, FALSE, and UNKNOWN. UNKNOWN silently excludes rows in WHERE, skips blocks in IF, and terminates loops in WHILE.
On this page
The short answer
The Boolean type in SQL Server is a distinct data kind produced by comparison operators, carrying exactly three values: TRUE, FALSE, and UNKNOWN. Unlike integer or string types, Boolean cannot be stored in a column or variable and cannot appear in a result set. UNKNOWN arises when NULL is involved in a comparison because NULL represents absent data, not a value. In WHERE, IF, and WHILE, UNKNOWN is treated as non-TRUE: rows are excluded, blocks are skipped, and loops terminate. The session-level setting SET ANSI_NULLS controls whether comparisons with NULL produce UNKNOWN or collapse to TRUE/FALSE.
The Boolean Data Type as a Distinct Kind in SQL Server
The Microsoft documentation for comparison operators states directly that the result of a comparison operator has the Boolean data type, with exactly three values: TRUE, FALSE, and UNKNOWN. This is not a behavioral quirk layered on top of a two-valued system. It is a type-system fact. The Boolean type in SQL Server is a distinct kind that exists alongside integer, string, and date types, but with a critical constraint: unlike those other types, a Boolean cannot be specified as the data type of a table column or variable, and it cannot be returned in a result set. You can never store a comparison result in a variable or display it in a SELECT output. The Boolean type exists only as an intermediate value consumed by filtering and control-flow constructs.
This constraint means that when you write a WHERE clause or an IF statement, the engine evaluates the Boolean expression internally and uses the result to make a decision. You cannot capture that decision as data. This distinguishes SQL Server from languages where a boolean is a first-class value you can assign, pass, and return.
-- The following are illustrative error examples, not runnable:
-- DECLARE @flag BIT = 1; -- BIT is an integer type, not Boolean
-- DECLARE @flag BOOLEAN; -- Error: 'BOOLEAN' is not a recognized data type
-- CREATE TABLE Flags (id INT, is_active BOOLEAN); -- Error
-- Live demonstration: comparison operators internally produce Boolean values:
SELECT CASE WHEN 1 = 1 THEN 'TRUE' ELSE 'FALSE' END AS Outcome;
-- The engine evaluates 1 = 1 as Boolean TRUE internally,
-- then CASE maps it to a string. You never see the Boolean itself.Practical tip
When debugging unexpected empty results or skipped logic, check whether a variable or column holds NULL and whether SET ANSI_NULLS is ON. Use IS NULL or IS NOT NULL for explicit null testing rather than relying on comparison operators.
Why UNKNOWN Exists: NULL Represents Absence of Data
UNKNOWN exists because NULL does not represent a value. NULL represents the absence of data, the fact that a value is unknown or not applicable. The Oracle documentation on nulls states this principle clearly: because null represents a lack of data, a null cannot be equal or unequal to any value or to another null. If you compare a column that contains NULL against a literal, the database engine cannot determine whether the missing value is greater, less, or equal. It does not guess. It returns UNKNOWN, the third Boolean value, to signal that the comparison is indeterminate rather than false.
The semantic distinction matters. FALSE means the comparison is definitively not satisfied. UNKNOWN means the comparison cannot be evaluated because one operand is missing. Treating these as equivalent would conflate a definitive mismatch with an indeterminate one, obscuring whether data is absent or simply does not satisfy the condition.
DECLARE @a INT = NULL;
DECLARE @b INT = 10;
-- Comparison with NULL produces UNKNOWN, not FALSE:
IF (@a = @b) PRINT 'Equal';
ELSE IF (@a <> @b) PRINT 'Not equal';
ELSE PRINT 'Neither: result was UNKNOWN';
-- Output: 'Neither: result was UNKNOWN'
-- IS NULL is the only way to test for the absence of data:
IF (@a IS NULL) PRINT 'a is NULL';
-- Output: 'a is NULL'Effect on WHERE: UNKNOWN Silently Excludes Rows
The WHERE clause filters rows by evaluating a Boolean expression for each row and returning only those rows where the expression is TRUE. When the expression evaluates to UNKNOWN, the row is excluded from the result set. The Oracle documentation describes this by noting that a condition evaluating to UNKNOWN acts almost like FALSE in a WHERE clause, returning no rows. The word almost is important here. The exclusion is real, but the mechanism differs: FALSE means the condition was tested and failed, while UNKNOWN means the condition was never testable. In practice, the observable outcome is identical: the row does not appear.
This silent exclusion is the most common source of confusion for developers. A query returns fewer rows than expected, and the missing rows are those where the compared column contains NULL. The query is syntactically valid and logically correct according to three-valued rules, but the developer assumed two-valued behavior.
CREATE TABLE #Products (ProductID INT, Price DECIMAL(10,2));
INSERT INTO #Products VALUES (1, 10.00), (2, NULL), (3, 20.00);
-- WHERE excludes rows where the condition is UNKNOWN:
SELECT ProductID FROM #Products WHERE Price > 5.00;
-- Returns: 1, 3 (row with NULL Price is silently excluded)
-- To include NULL rows, use explicit null testing:
SELECT ProductID FROM #Products WHERE Price > 5.00 OR Price IS NULL;
-- Returns: 1, 2, 3Effect on IF: UNKNOWN Does Not Enter the Branch
The Microsoft documentation explicitly lists IF as a consumer of Boolean expressions, alongside WHERE. The IF statement requires the Boolean expression to evaluate to TRUE for the subsequent block to execute. An UNKNOWN result causes the IF to behave as though the condition were FALSE: the block is skipped entirely. There is no third branch in T-SQL IF that handles UNKNOWN separately.
This means a conditional like IF (@val <> 0) where @val is NULL will not enter the block, even though the comparison is indeterminate rather than false. The developer who intended the block to run when the value is unknown will find it silently skipped.
DECLARE @val INT = NULL;
-- IF does not enter the block when the condition is UNKNOWN:
IF (@val > 0)
PRINT 'Positive';
ELSE IF (@val < 0)
PRINT 'Negative';
ELSE
PRINT 'Zero or UNKNOWN';
-- Output: 'Zero or UNKNOWN'
-- The ELSE branch catches both FALSE and UNKNOWN.Effect on WHILE: UNKNOWN Terminates the Loop
The WHILE loop continues only while the Boolean expression evaluates to TRUE. If the expression becomes UNKNOWN, the loop exits immediately as though the condition were FALSE. This is a distinct consequence from WHERE and IF because it can occur mid-execution. A loop that processes rows one by one may partially complete its work and then terminate prematurely when a NULL value enters the comparison, leaving the system in an inconsistent state that is harder to diagnose than a single excluded row.
The Microsoft documentation confirms that control-of-flow language statements such as IF and WHILE consume Boolean expressions from comparison operators. The same three-valued logic governs loop entry and continuation.
DECLARE @counter INT = 0;
DECLARE @threshold INT = NULL;
-- WHILE terminates immediately because @counter < @threshold is UNKNOWN:
WHILE (@counter < @threshold)
BEGIN
SET @counter = @counter + 1;
END;
SELECT @counter AS FinalCounter;
-- Returns 0. The loop never executed.
-- Compare with a known threshold:
DECLARE @known INT = 3;
SET @counter = 0;
WHILE (@counter < @known)
BEGIN
SET @counter = @counter + 1;
END;
SELECT @counter AS FinalCounter;
-- Returns 3. Loop ran three times.SET ANSI_NULLS Modifies When UNKNOWN Appears
SQL Server provides a session-level toggle, SET ANSI_NULLS, that directly controls whether three-valued comparison behavior manifests. When SET ANSI_NULLS is ON, the default in modern SQL Server and Azure SQL Database contexts, an operator with one or two NULL expressions returns UNKNOWN. When SET ANSI_NULLS is OFF, the equals and not-equals operators treat NULL as a known value equivalent to any other NULL, returning only TRUE or FALSE and never UNKNOWN.
This setting is deprecated and may be removed in future versions, but legacy code or connections that set it to OFF will not exhibit three-valued behavior for equality comparisons. Understanding this toggle is necessary when reading older codebases or diagnosing why the same query behaves differently across connections.
-- With ANSI_NULLS ON (default):
SET ANSI_NULLS ON;
DECLARE @x INT = NULL;
IF (@x = @x) PRINT 'Equal';
ELSE IF (@x <> @x) PRINT 'Not equal';
ELSE PRINT 'UNKNOWN';
-- Output: 'UNKNOWN'
-- With ANSI_NULLS OFF (legacy behavior):
SET ANSI_NULLS OFF;
IF (@x = @x) PRINT 'Equal';
ELSE IF (@x <> @x) PRINT 'Not equal';
ELSE PRINT 'UNKNOWN';
-- Output: 'Equal'
-- NULL is treated as a known value equal to another NULL.
SET ANSI_NULLS ON; -- Restore defaultConcrete Illustration: Same Condition, Three Contexts
The following example declares a NULL integer and uses the same comparison expression @val <> 0 in three different contexts. The Boolean value produced is UNKNOWN in all three cases, but the observable consequence differs by context: WHERE returns no rows, IF prints nothing, and WHILE never enters the loop body.
The key insight is that the underlying Boolean value is identical. The difference lies in how each construct interprets non-TRUE results. WHERE excludes the row, IF skips the block, and WHILE terminates the loop. All three treat UNKNOWN as insufficient to proceed.
-- Boolean expression with NULL: @val IS NULL, so @val <> 0 evaluates to UNKNOWN
DECLARE @val INT = NULL;
-- WHERE: row excluded (UNKNOWN treated as non-TRUE)
SELECT 1 AS Result WHERE @val <> 0;
-- Returns no rows.
-- IF: block skipped (UNKNOWN treated as non-TRUE)
IF (@val <> 0)
PRINT 'Entered IF';
-- Prints nothing.
-- WHILE: loop exits immediately (UNKNOWN treated as non-TRUE)
DECLARE @i INT = 1;
WHILE (@val <> 0)
BEGIN
SET @i = @i + 1;
END
-- @i remains 1; loop never executed.Applicability Limits and Scope Boundaries
This guide covers SQL Server Boolean type semantics specifically. Oracle and other engines implement three-valued logic but with different type-system constraints and settings. The Boolean type constraint that it cannot be a column or variable type is SQL Server specific and does not apply to all RDBMS implementations.
The guide does not prescribe IS NULL and IS NOT NULL as the correct testing approach; that is covered in existing articles on three-valued logic conditions. It does not discuss Oracle-specific DECODE behavior where two nulls are considered equal, nor compound-key equality exceptions. It does not cover the NOT UNKNOWN propagation chain where negating UNKNOWN yields UNKNOWN rather than TRUE. SET ANSI_NULLS OFF behavior is described for understanding legacy code, not as a recommendation.
-- SQL Server: Boolean is an internal type only, never a column or variable type.
-- Oracle: Same three-valued logic, but no ANSI_NULLS toggle.
-- PostgreSQL: Has a native BOOLEAN type that CAN be stored in columns.
-- CREATE TABLE flags (id INT, is_active BOOLEAN);
-- INSERT INTO flags VALUES (1, TRUE), (2, NULL), (3, FALSE);
-- SELECT * FROM flags WHERE is_active = TRUE; -- returns only row 1
-- The three-valued WHERE behavior is shared, but the type-system constraint
-- (Boolean cannot be a column type) is specific to SQL Server.Things to check
- The Boolean data type in SQL Server has exactly three values: TRUE, FALSE, and UNKNOWN
- Boolean cannot be used as a column type, variable type, or returned in a result set
- WHERE returns only rows where the Boolean expression is TRUE; UNKNOWN rows are excluded
- IF executes its block only when the Boolean expression is TRUE; UNKNOWN skips the block
- WHILE continues only while the Boolean expression is TRUE; UNKNOWN terminates the loop
- SET ANSI_NULLS ON causes NULL comparisons to return UNKNOWN; OFF collapses to TRUE or FALSE for equality operators
Where this applies
This guide covers SQL Server Boolean type semantics and its effect on WHERE, IF, and WHILE. It does not address IS NULL and IS NOT NULL as the correct null-testing mechanism, Oracle-specific DECODE or compound-key exceptions, or the NOT UNKNOWN propagation chain. SET ANSI_NULLS OFF is deprecated and described only for legacy code understanding. The Boolean type constraint is SQL Server specific and may differ in other engines.