Discussion Locked

SELECT NULL returning BIT values

0 helpful
8 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

I just want to make a SELECT that returns 1 (one) if the field is NULL and 0 (zero) if the field is NOT NULL. The fields are varchar or datetime. I've tried something like that but it did'nt works:
SELECT BirthDate IS NULL, Name IS NULL FROM CUSTOMERS
Thanks in advance for any help.

8 replies

by peyison

You could try something like:

SELECT IIF(Isnull(BirthDay),1,0) as BirthDayBit
FROM Customers

Hope this helps.

by peyison

Actually the first answer was for Access. IsNull takes one parameter - you may be thinking of NullIf().

You may have to do something like:

Select 0 as BirthDayBit From Customers Where BirthDate Is Not Null

Union

Select 1 as BirthDayBit From Customers Where BirthDate Is Null

Actually - you can probably do it your way:

Select (BirthDate Is Null) As BirthdateBit From Customers

by maurodelfino2000

This was one of my first tries, and uncredible, MS SQL Server 7.0 don't accept it. I says "Incorrect syntax near the keyword 'IS'". IS NULL only works well in WHERE clause or IF command...
Thanks any way...

by peyison

Did you try the first query in answer 2 - using the Union? That has the Is Null in the Where clause.

by maurodelfino2000

Yes, but i need to select more than one field at once, like the sample query i've posted in the question.
(SELECT BirthDate IS NULL, Name IS NULL FROM CUSTOMERS). The logic in answer 2 woks only for one field.

by hclarius

RETURN VALUE IN FIELDX FROM LOOKING AT FIELDY IN TABLE1 (NO FILTERS)

SELECT
FIELDX=
Case
WHEN FIELDY IS NULL 1
ELSE 0
END
FROM TABLE1 WHERE....etc

This discussion is closed to new replies.