SQL Server Bit Data Type

“Data types are the building block for anything you build with any programming language or data you store in a database. They are responsible for determining what values you hold, the structure of the data, the formatting, what functions and operations you can perform, the storage options, and much more.

Various data types may vary depending on the programming language and more. However, for this post, we will focus on the Bit data type available in SQL Server.”

SQL Server Bit Type

The bit data type is an integer data type that accepts three values; 0, 1, and NULL. The following is the syntax of the BIT type.

BIT

SQL Server will automatically optimize the size of columns that store bit data type. For example, if there are eight or fewer bits in a column, SQL Server will store them as 1 byte. If the number of bits is eight or higher, it will store them as 2 bytes.

It is good to remember that Boolean values can be converted to bit types with TRUE resolving to 1 and FALSE resolving to 0.

Example

The following illustrates a create table statement with a bit data type column.

CREATE TABLE temp_table(
    id INT NOT NULL IDENTITY PRIMARY KEY,
    bit_data bit
);

You can then insert sample data into the table as:

INSERT
    INTO
    TEMP_TABLE(BIT_DATA)
VALUES (0),
(1),
(0),
(1),
(0);

Select data from the table:

SELECT * FROM temp_table;

Output:

id|bit_data|
--+--------+
1|       0|
2|       1|
3|       0|
4|       1|
5|       0|

Ended

This short post covered the basics of working with the bit type in SQL Server.

Thanks for reading!!



from https://ift.tt/JeECkMW

Post a Comment

0 Comments