Search code examples
sqlsql-serverazure-sql-database

How do I create a calculated boolean column with a SELECT statement


I'm trying to get a true/false column in my select statement to tell me if two other columns are equal

SELECT 
    dol.RequestedBy = dol.RequestedFor AS "EQ",
    dol.*
FROM DaysOnLeave AS dol WHERE Status = 'Approved';

But having the = there gives a Incorrect Syntax error.

RequestedBy and RequestedFor are both foreign keys to the same table (Numeric)


Solution

  • The syntax you're trying to use does not work in SQL Server. Also there is no boolean datatype in SQL Server. Use case expression for this:

    SELECT CASE WHEN dol.RequestedBy = dol.RequestedFor THEN 1 ELSE 0 END AS "EQ", ...