liveBook · Manning

Bit data type

As explained in 100 SQL Server Mistakes and How to Avoid Them

BIT is a SQL Server integer data type that stores 0, 1, or NULL and represents Boolean values.

1. Mistake: storing every flag as INT. SQL Server BIT stores only 0, 1, or NULL, making it suitable for Boolean values such as whether an employee is staff or a contractor. It also reduces wasted storage, especially in large tables or indexed columns. section 3.1

Remember these conversion rules:

  • TRUE and FALSE become 1 and 0 automatically. section 3.1
  • Any numeric value other than 0 or 1 becomes 1, so unexpected inputs can silently mean true. section 3.1
  • The first eight BIT columns use one byte; each subsequent group of eight adds another byte. section 3.1

Employees table

Define a flag with BIT when its values represent a binary choice: `StaffOrContract BIT NOT NULL`. For example, `SELECT CAST(86.2 AS BIT)` returns 1, demonstrating why input validation still matters. section 3.1

How 100 SQL Server Mistakes and How to Avoid Them covers it

How deep it goes
Explains allowed values, implicit conversions, multi-column storage, and a table definition without covering indexing internals.
Examples
Uses StaffOrContract, CAST(86.2 AS BIT), and a recreated Employees table as concrete examples.
What it assumes
Assumes basic SQL table and data-type knowledge while introducing BIT as an additional integer type.
What you can do after
Build it from scratch in a SQL Server table and recognize conversion behavior that can turn unexpected numbers into 1.
How it connects
Connects BIT to Boolean values, NULL, integer data types, table design, and storage efficiency.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of 100 SQL Server Mistakes and How to Avoid Them. The section links point to the parts of the book it draws on.