views:

120

answers:

2

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]
+3  A: 

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)
)
Mark Byers
+1  A: 

SQL Server doesn't have any native true boolean data type (in the sense that you could use a variable in place of a boolean expression, like select * from Item where IsSpecialItem). The only way you can represent it is with something like Mark suggests, using reserved values (in this case, your query would be select * from Item where IsSpecialItem = 1).

Adam Robinson