Fix Error 4145 “An expression of non-boolean type specified in a context where a condition is expected” in SQL Server

If you’re getting SQL Server error 4145 that goes something like “An expression of non-boolean type specified in a context where a condition is expected…“, it’s probably because you’re using a boolean-like syntax in the wrong context.

This error occurs when a non-boolean expression is used in a context where a condition (i.e., something that evaluates to true or false) is expected. This often happens in IF statements, CASE expressions, or WHERE clauses when a value that isn’t a boolean is mistakenly used as a condition.

Even when we do use a boolean value, we can still get this error if we use it in the wrong context.

Example of Error

Here’s an example of code that produces the error:

SELECT FirstName FROM Employees WHERE EmployeeID;

Result:

Msg 4145, Level 15, State 1, Line 1
An expression of non-boolean type specified in a context where a condition is expected, near ';'.

In this case, I used a WHERE clause in my query, but it wasn’t a condition. I should have passed a condition.

Here’s how we can get the error when using the CASE statement:

DECLARE @Value INT = 10;
SELECT 
    CASE 
        WHEN @Value THEN 'It is 10'
        ELSE 'It is not 10'
    END AS Result;

Result:

Msg 4145, Level 15, State 1, Line 4
An expression of non-boolean type specified in a context where a condition is expected, near 'THEN'.

Similarly, we didn’t provide a condition where we should have. The error specifically states that an expression of non-boolean type was specified where a condition was expected.

But merely passing a boolean value isn’t enough. We still need to use the correct syntax. Let’s change that example so that we’re passing a boolean value instead:

DECLARE @Value BIT = 1;
SELECT 
    CASE 
        WHEN @Value THEN 'It is TRUE'
        ELSE 'It is FALSE'
    END AS Result;

Result:

Msg 4145, Level 15, State 1, Line 4
An expression of non-boolean type specified in a context where a condition is expected, near 'THEN'.

Same error. That’s because when we use this syntax, the CASE expression expects a boolean expression (not simply a boolean value). When we use a condition/boolean expression, it’s called a searched CASE expression. When we compare an expression to a set of simple expressions, it’s a simple CASE expression. Each of these has its own syntax.

Solution

To fix this issue, we need to pass a condition instead.

Here’s an example of fixing the first example:

SELECT FirstName FROM Employees WHERE EmployeeID = 1;

Result:

FirstName
---------
Hazy

All I did was append = 1 to the query. In other words, I used a condition in the WHERE clause.

Here’s an example of fixing the second example:

DECLARE @Value INT = 10;
SELECT 
    CASE 
        WHEN @Value = 10 THEN 'It is 10'
        ELSE 'It is not 10'
    END AS Result;

Result:

It is 10

Here, I added = 10 to the WHEN clause in order to make it a condition. The CASE expression still remains a searched CASE expression.

And let’s fix the one with the boolean value:

DECLARE @Value BIT = 1;
SELECT 
    CASE @Value
        WHEN 1 THEN 'It is TRUE'
        ELSE 'It is FALSE'
    END AS Result;

Result:

It is TRUE

This time we turned it into a simple CASE expression. We did this by moving @Value up to the previous line, and then providing a value in the WHEN clause for which to test it against.

I should point out that we didn’t necessarily need to convert it to a simple CASE expression. We could’ve left it as a searched CASE expression. But if we had, then we would’ve needed to use a condition. For example:

DECLARE @Value BIT = 1;
SELECT 
    CASE 
        WHEN @Value = 1 THEN 'It is TRUE'
        ELSE 'It is FALSE'
    END AS Result;

Result:

It is TRUE

Same result.