In my database I have some computed columns to help ensure referential integrity constraints. I use computed columns and not default-value columns because LINQ2SQL, which is what I use, does not understand default values from the database.
My attempt is to use
CAST('C' as CHAR(1))
which is automatically converted to
(CONVERT([char](1),'C',0))
by SSMS. However, this results in a NULL-able CHAR(1) column type.
If I just use 'C'
or ISNULL(NULL,'C')
(which does result in a non-NULL type) the column is picked up as a VARCHAR(?). And, if I combine the two to use ISNULL(NULL,CONVERT([char](1),'C',0))
, I'm back to a NULL-able CHAR(1).
There are two reasons I want this:
The computed column will participate in a relation with a foreign CHAR(1) column.
A non-NULL CHAR(1) maps directly to the .NET Character type in LINQ2SQL.
Update:
It Works For Me with ISNULL(CONVERT([char](1),'C',0),0)
, but I'm not really sure "why". If anything, it seems like ISNULL(..,0)
would un-unifiy the type further.
I would be more than happy for an answer with a good explanation.