Bazinga
Bazinga

Reputation: 1014

How do I incorporate "where [binary] is not null" in a T-SQL select statement?

I have a stored procedure that I need to filter rows that have a binary value and return only rows that are not null in the binary column.

When I execute this stored procedure:

create procedure sp_GetGraduatingStudentDataByYear
(
    @year nvarchar(4)
)
as
select * from Cohort_Graduation_Student_Data where Exp_Grad_Year = @year and Off_Track != null

go

I get no results.

How can I alter this script to return the rows with a null value in the binary column?

Upvotes: 1

Views: 1533

Answers (1)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726509

This is not because it's a binary column, but because null in SQL should not be compared to anything. Essentially, Off_Track != null condition filters out all rows - all checks for column = null and column != null always evaluate to false.

Use is not null instead:

select *
from Cohort_Graduation_Student_Data
where Exp_Grad_Year = @year and Off_Track is not null

Upvotes: 4

Related Questions