mpapec
mpapec

Reputation: 50647

PostgreSQL boolean cast (0 as false)

I prefer 1/0 instead of t/f, so what should I use when converting boolean to integer?

select coalesce((null::boolean)::int, 0)

OR

select case null::boolean when 't' then 1 else 0 end

... something else?

Upvotes: 14

Views: 60817

Answers (1)

David Aldridge
David Aldridge

Reputation: 52356

Regardless of which you do, a Boolean null does not equal false, any more than a numeric null equals zero.

Try:

Cast(col1 as integer)

If you really wanted to treat null as false then:

case when col1 then 1 else 0 end

It would be a Bad Thing though

Upvotes: 24

Related Questions