Using comparison operators in SELECT clause of T-SQL query

abatishchev picture abatishchev · Feb 24, 2010 · Viewed 37.5k times · Source

How to select a result of comparison operator as a field with type BIT?

How it does work in C#:

bool isGreater = FieldA > FieldB;

How it doesn't work in T-SQL:

SELECT (FieldA > FieldB) AS BIT FROM t

How to write such task properly?

Answer

Rockcoder picture Rockcoder · Feb 25, 2010

You should use CASE clause:

CASE
    WHEN FieldA > FieldB THEN 1
    ELSE 0
END AS [BIT]