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

SqlSql Server-2000

Sql Problem Overview


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?

Sql Solutions


Solution 1 - Sql

You have to use a CASE statement for this:

SELECT CASE WHEN columnName IS NULL THEN 'false' ELSE 'true' END FROM tableName;

Solution 2 - Sql

Or you can do like this:

    SELECT RealColumn, CAST(0 AS bit) AS FakeBitColumn FROM tblTable

Solution 3 - Sql

If you need a output as boolean

CAST(CASE WHEN colName IS NULL THEN 0  ELSE 1   END as BIT) aIsBooked

Solution 4 - Sql

for the column in the view you can use something like

CASE WHEN ColumnName is not null THEN 'True' ELSE 'False' END

or in a statement

SELECT 
s.ID,
s.[Name],
CASE WHEN s.AchievedDate is not null THEN 'True' ELSE 'False' END [IsAchieved]
FROM Schools s

or for further processing afterwards I would personally use

SELECT 
s.ID,
s.[Name],
CASE WHEN s.AchievedDate is not null THEN 1 ELSE 0 END [IsAchieved]
FROM Schools s

Solution 5 - Sql

I had a similar issue where I wanted a view to return a boolean column type based on if an actual column as null or not. I created a user defined function like so:

CREATE FUNCTION IsDatePopulated(@DateColumn as datetime)
RETURNS bit
AS
BEGIN
	DECLARE @ReturnBit bit;
	
	SELECT @ReturnBit = 
		CASE WHEN @DateColumn IS NULL 
			THEN 0 
			ELSE 1 
		END
		
	RETURN @ReturnBit
END

Then the view that I created returns a bit column, instead of an integer.

CREATE VIEW testView
AS
	SELECT dbo.IsDatePopulated(DateDeleted) as [IsDeleted] 
	FROM Company

Solution 6 - Sql

You asked for boolean, which we call bit in t-sql.

Other answers have either given you a varchar 'true' and 'false' or 1 and 0. 'true' and 'false' are obviously varchar, not boolean. I believe 1 and 0 would be cast as an integer, but it's certainly not a bit. This may seem nit-picky, but types matter quite often.

To get an actual bit value, you need to cast your output explicitly as a bit like:

select case when tableName.columnName IS NULL then cast(0 as bit) else cast(1
as bit) END as ColumnLabel from tableName

Solution 7 - Sql

I think this is slightly simpler then the other solutions:

SELECT Cast(ISNULL([column name], 0) AS BIT) AS IsWhatever

Solution 8 - Sql

Since SQL server 2012 you can use IIF

IIF(columnName IS NULL, 'false', 'true')

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAnheledirView Question on Stackoverflow
Solution 1 - SqlAdam BellaireView Answer on Stackoverflow
Solution 2 - SqlSchnapzView Answer on Stackoverflow
Solution 3 - SqlMaheshView Answer on Stackoverflow
Solution 4 - SqltocsoftView Answer on Stackoverflow
Solution 5 - SqllcrepasView Answer on Stackoverflow
Solution 6 - SqlSteve SetherView Answer on Stackoverflow
Solution 7 - SqltodjiView Answer on Stackoverflow
Solution 8 - SqlMirek MichalakView Answer on Stackoverflow