PostgreSQL 布尔型转换(0 为假)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16142438/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PostgreSQL boolean cast (0 as false)
提问by Сухой27
I prefer 1/0 instead of t/f, so what should I use when converting boolean to integer?
我更喜欢 1/0 而不是 t/f,那么在将布尔值转换为整数时应该使用什么?
select coalesce((null::boolean)::int, 0)
OR
或者
select case null::boolean when 't' then 1 else 0 end
... something else?
……还有什么?
回答by David Aldridge
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:
如果您真的想将 null 视为 false,那么:
case when col1 then 1 else 0 end
It would be a Bad Thing though
虽然这将是一件坏事