Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to output a boolean in T-SQL based on the content of a column?

I made a view to abstract columns of different tables and pre-filter and pre-sort them. There is one column whose content I don't care about but I need to know whether the content is null or not. So my view should pass an alias as "true" in case the value of this specified column isn't null and "false" in case the value is null.

How can I select such a boolean with T-SQL?

like image 444
Anheledir Avatar asked Oct 01 '08 11:10

Anheledir


People also ask

How do you return a boolean value in SQL?

SQL Server does not support a Boolean type e.g. SELECT WHEN CAST(1 AS BIT) THEN 'YES' END AS result -- results in an error i.e. CAST(1 AS BIT) is not the same logical TRUE.

How do you create a boolean column in SQL?

In SQL Server, a Boolean Datatype can be created by means of keeping BIT datatype. Though it is a numeric datatype, it can accept either 0 or 1 or NULL values only. Hence easily we can assign FALSE values to 0 and TRUE values to 1. This will provide the boolean nature for a data type.

What type of results can a Boolean expression have in SQL?

A Boolean expression can consist of Boolean data, such as the following: BOOLEAN values ( YES and NO , and their synonyms, ON and OFF , and TRUE and FALSE )


1 Answers

You have to use a CASE statement for this:

SELECT CASE WHEN columnName IS NULL THEN 'false' ELSE 'true' END FROM tableName; 
like image 103
Adam Bellaire Avatar answered Sep 18 '22 12:09

Adam Bellaire