61

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?

8 Answers 8

89

You have to use a CASE statement for this:

SELECT CASE WHEN columnName IS NULL THEN 'false' ELSE 'true' END FROM tableName;
4
  • 2
    This results in a varchar(5)-column on the view for me, not a boolean. What am I doing wrong? Commented Jun 25, 2013 at 10:46
  • 2
    @uffjohn there's no such thing as "true" or "false" in Sql. Normally it's either 1 or 0, a "bit" flag. If you're using this in .NET code or something, you could cast this value to a boolean there.
    – user344450
    Commented Aug 15, 2013 at 19:41
  • 25
    Or do CAST(CASE WHEN columnName IS NULL THEN 0 ELSE 1 END AS BIT) FROM tableName Commented Oct 2, 2014 at 21:15
  • This is just flat out wrong. 'true' is not boolean. Labeling the column as bit is also not a bit. Commented Feb 3, 2016 at 20:36
31

Or you can do like this:

    SELECT RealColumn, CAST(0 AS bit) AS FakeBitColumn FROM tblTable
2
  • 5
    This is what worked for me, specifically with a CAST(CASE WHEN <predicate> THEN 1 ELSE 0 END) AS BIT). Commented Jun 25, 2013 at 10:50
  • 2
    CAST(0 AS bit) is what i was looking for.
    – psulek
    Commented Jun 6, 2014 at 15:57
20

If you need a output as boolean

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

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
6

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
5

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
1
  • Nice,useful and correct: just used for having an autogenerating WPF DataGrid's DataGridCheckBoxColumn populated with correct data. Commented Nov 21, 2017 at 18:48
1

I think this is slightly simpler then the other solutions:

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

1
  • That does not work on T-SQL on non numeric columns as you get a casting error Commented Aug 28, 2020 at 10:59
1

Since SQL server 2012 you can use IIF

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.