Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Computed bit column that returns whether another column is null

I try to have this computed column:

CREATE TABLE dbo.Item
(
    ItemId int NOT NULL IDENTITY (1, 1),
    SpecialItemId int NULL,
    --I tried this
    IsSpecialItem AS ISNULL(SpecialItemId, 0) > 0, 
    --I tried this
    IsSpecialItem AS SpecialItemId IS NOT NULL
    --Both don't work
)  ON [PRIMARY]
like image 223
Shimmy Weitzhandler Avatar asked Dec 06 '09 04:12

Shimmy Weitzhandler


1 Answers

This works:

CREATE TABLE dbo.Item
(
    ItemId int NOT NULL IDENTITY (1, 1),
    SpecialItemId int NULL,
    IsSpecialItem AS
        CAST(CASE ISNULL(SpecialItemId, 0) WHEN 0 THEN 0 ELSE 1 END AS bit)
)
like image 188
Mark Byers Avatar answered Sep 28 '22 00:09

Mark Byers