What is the best practice for creating a yes/no
i.e. Boolean
field when converting from an access database
or in general?
11 Answers
There are already answers saying use of Bit. I will add more to these answers.
You should use bit for representing Boolean values.
Remarks from MSDN article.
Bit can take a value of 1, 0, or NULL.
The SQL Server Database Engine optimizes storage of bit columns. If there are 8 or less bit columns in a table, the columns are stored as 1 byte. If there are from 9 up to 16 bit columns, the columns are stored as 2 bytes, and so on.
The string values TRUE and FALSE can be converted to bit values: TRUE is converted to 1 and FALSE is converted to 0.
Converting to bit promotes any nonzero value to 1.
Note: It is good practice to keep values as 1 and 0 only with data type NOT NULL
As Bit have values 1, 0 and NULL. See truth table for this. So plan values accordingly. It might add confusion by allowing NULL value for bit data type.
In SQL Server Management Studio of Any Version, Use
BIT
as Data Type
which will provide you with True
or False
Value options. in case you want to use Only 1
or 0
then you can use this method:
CREATE TABLE SampleBit(
bar int NOT NULL CONSTRAINT CK_foo_bar CHECK (bar IN (-1, 0, 1))
)
But I will strictly advise BIT
as The BEST Option. Hope fully it's help someone.