How to use BIT data type in Sql Server?
BIT data type is a special kind of integer that could be either true or false. It is used to store 0,1 or Null,
We could use this data type to keep track of whether a user accound is Enabled or Disabled. You can use it like a BOOLEAN variable.
Possible Values:
0 - FALSE
1 - TRUE
NULL
CREATE TABLE USER_ACCOUNT
(
USER_NAME VARCHAR(256) NOT NULL,
IS_ENABLED BIT
)
When we insert a record to this table, SQL SERVER stores 0, 1 or NULL in the IS_ENABLED column.The value 0 represents 'FALSE' and 1 represents 'TRUE'.When using the value 'TRUE' or 'FALSE', remember to enclose the value with single quotes.
Following commands are used to insert records to the USER_ACCOUNT table:
In this first example, we insert a value 'FALSE' into the table, Sql Server stores 'FALSE' as 0 in the BIT datatype column
INSERT INTO USER_ACCOUNT VALUES ('MIC','FALSE')
Next, we enable the user by updating IS_ENABLED BIT value from 0 to 1. Following command inserts value 'TRUE' into the BIT column
UPDATE USER_ACCOUNT SET IS_ENABLED='TRUE' WHERE USER_NAME='MIC'
Also we could use interger values to insert or update the BIT data type.
UPDATE USER_ACCOUNT SET IS_ENABLED=1 WHERE USER_NAME='MIC'
Alternate Titles: BIT in T-SQl, BIT in WHERE clause, BOOLEAN in SQL SERVER, BOOL in SQL SERVER