SQL BIT Data Type

The BIT data type is an integer value that accepts 0, 1, and NULL.

BIT represents a boolean type with TRUE (1) and FALSE (0) values.

String values 'TRUE' and 'FALSE' are also accepted and converted to 1 and 0.

Example

#

A table with a BIT column.

CREATE TABLE DemoTable  
( 
  Id INT IDENTITY, 
  MyBoolean BIT
);
GO  

INSERT INTO DemoTable VALUES (1);  
INSERT INTO DemoTable VALUES (0); 
INSERT INTO DemoTable VALUES ('TRUE');  
INSERT INTO DemoTable VALUES ('FALSE');
INSERT INTO DemoTable VALUES (NULL); 
GO  

SELECT * FROM DemoTable;
GO

DROP TABLE DemoTable;
GO
Result:  5 records
Id MyBoolean
1 1
2 0
3 1
4 0
5 NULL

More Examples

BIT with BOOLEAN STRING

PRODUCT
Id
ProductName
SupplierId
UnitPrice
Package
IsDiscontinued
Problem: List all discontinued products.
SELECT ProductName, IsDiscontinued
  FROM Product
 WHERE IsDiscontinued = 'TRUE'
Result:  8 records
ProductName IsDiscontinued
Chef Anton's Gumbo Mix 1
Mishi Kobe Niku 1
Alice Mutton 1
Guaraná Fantástica 1
Rössle Sauerkraut 1
Thüringer Rostbratwurst 1
Singaporean Hokkien Fried Mee 1
Perth Pasties 1

Note: The 'TRUE' boolean string has been converted to 1. BIT values returned from the database are always 0, 1, or NULL.


You may also like



Guides


vsn 3.1