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

前端 未结 10 1226
情话喂你
情话喂你 2021-02-06 21:07

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

  • Any non-null value is TRUE in the results
  • Any
10条回答
  •  梦如初夏
    2021-02-06 21:40

    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 
    

提交回复
热议问题