在SQL查询中将int或null转换为布尔值的最佳方法是什么? [英] What is the best way to convert an int or null to boolean value in an SQL query?

查看:786
本文介绍了在SQL查询中将int或null转换为布尔值的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在SQL查询中将int或null转换为布尔值的最佳方法是什么,

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

  • 结果中任何非空值为 TRUE
  • 结果中任何空值为 FALSE
  • Any non-null value is TRUE in the results
  • Any null value is FALSE in the results

推荐答案

据我所知(如果我错了,请纠正我),SQL中没有文字布尔值的概念.您可以让表达式评估为布尔值,但不能输出它们.

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.

这就是说,您可以使用CASE WHEN生成可以在比较中使用的值:

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 

这篇关于在SQL查询中将int或null转换为布尔值的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆