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?

+5
source share
1 answer

No matter what you do, the Boolean zero is not false, greater than the numeric zero is zero.

Try:

Cast(col1 as integer)

If you really wanted to treat null as false, then:

case when col1 then 1 else 0 end

That would be bad thing though

+12
source

All Articles