I have a table, with Id
as the primary key.
create table Anything
(
Id bigint not null primary key identity(1, 1)
)
When I look at this table in Object Explorer
, I see this image of course:
As you can see, column Id
is not null.
Then I create a dummy view on this dummy table:
create view IdIsTwoView
as
select
Id,
(
case
when Id = 2
then cast(1 as bit)
else cast(0 as bit)
end
) as IdIsTwo
from Anything
But this time, in Object Explorer I see this result:
As you can see, in spite of this fact that my case
clause is all-encompassing and covers 100% of all records, and has an answer for all records, it's nullable.
Why does SQL Server have this weird behavior? And how do I force it to be not null?
P.S we have an infrastructure to generate code dynamically, and this behavior causes us trouble and we have to manually go and change all bool?
types in C# to bool
.
Best Answer
To answer the why part of your question, the
CASE
expression result is nullable because theCAST
function result is nullable. This can be observed with:Result (abbreviated):
The result is not null when a constant is specified without the function:
But the result data type is
int
instead of the desiredbit
as SQL Server has nobit
constant without implicit or explict conversion,ISNULL
is needed to coerce a not null result as @MichaelGreen answered.