Thomas Bratt
Thomas Bratt

Reputation: 51812

What is the best way to convert an int or null to boolean value in an SQL query?

What is the best way to convert an int or null to boolean value in an SQL query, such that:

Upvotes: 62

Views: 146877

Answers (11)

Go Man
Go Man

Reputation: 67

We can check if is null return "False" else return "True"

SELECT IIF(ISNULL(ColumnName<Nullable>, 'true')='false', 'true', 'false') ;


SELECT IIF(ISNULL(Email, 'true')='false', 'true', 'false') from Employee ;

Upvotes: 1

Daniel Hudsky
Daniel Hudsky

Reputation: 301

The syntax works, but I had to figure out how to place it in my query. If OK, I'd like to share an example on how to fit into an extended query:

select count(*) as count, inventory,
CASE WHEN inventory = 0 THEN 'empty' ELSE 'not empty' END as InventoryStatus
from mytable group by count, inventory

Upvotes: -1

David Castro
David Castro

Reputation: 1977

Usually when use 1, it means is true and else in other case.

So:

SELECT  IsDefault = CASE WHEN IsDefault = 1 THEN 'true' ELSE 'false' END
FROM table

Upvotes: 1

PL_kolek
PL_kolek

Reputation: 375

The shortest one I know for Oracle:

SELECT NVL2(nullableColumn, 1, 0) FROM someTable

NVL2(value, ifNotNull, ifNull) returns ifNotNull if the value is not null, and ifNull otherwise.

Upvotes: 0

WW.
WW.

Reputation: 24291

In Oracle, assuming you use 0 for false and 1 for true:-

SELECT DECODE( col, NULL, 0, 1) FROM ...

You can also write this using the CASE syntax, but the above is idiomatic in Oracle. DECODE is a bit like a switch/case; if col is NULL then 0 is returned, else 1.

Upvotes: 1

kristof
kristof

Reputation: 53834

Assuming you want 0,1 value as a return, and that we are talking about integer I would use the logic specified by Torbjörn and wrap it in the function

create function dbo.isNotNull(@a int)
returns bit
as
begin
return isnull(@a-@a+1,0)
end

so then you can use it whenever you need by simply calling

select dbo.isNotNull(myIntColumn) from myTable

The answer provided by Tomalak is more universal though as it would work with any data type

Upvotes: 4

Patrick Parent
Patrick Parent

Reputation: 381

You may want to do a Convert(BIT, Value) of your result. Because something SQL will return an error that the value is not a boolean.

Upvotes: 5

cvk
cvk

Reputation: 1000

No need to use case... when:

select (column_name is not null) as result from table_name;

Returns 1 for all fields not NULL and 0 for all fields that are NULL, which is as close as you can get to booleans in SQL.

Upvotes: 35

mattlant
mattlant

Reputation: 15451

SELECT 
    CASE
        WHEN thevalue IS NULL THEN 0
        ELSE 1
    END AS newVal
FROM .... (rest of select)

I think it goes something like this

Actually, the ISNULL, may need to be WHEN thevalue IS NULL THEN 0

Upvotes: 7

Tomalak
Tomalak

Reputation: 338228

To my knowledge (correct me if I'm wrong), there is no concept of literal boolean values in SQL. You can have expressions evaluating to boolean values, but you cannot output them.

This said, you can use CASE WHEN to produce a value you can use in a comparison:

SELECT 
    CASE WHEN ValueColumn IS NULL THEN 'FALSE' ELSE 'TRUE' END BooleanOutput 
FROM 
    table 

Upvotes: 80

Torbj&#246;rn Gyllebring
Torbj&#246;rn Gyllebring

Reputation: 18228

isnull(column - column + 1, 0) != 0

Upvotes: 2

Related Questions