SQL - DataTypes - Bit

The bit data type is simple - and yet, it gets kind of tricky.

In short - this datatype can be set to 0 or 1.

Two things complicate this:

  • It can also be set to null
  • Many programming languages will set this to true/false

Consequently - it’s pretty easy to fall into some traps.

Here are a few

asp.net sample - populating a checkbox based on a bit field

1
2
3
4
5
6
7
8
9
10
11
ds = dl_db.DS_Fetch(str, "ConnectionString")
chkChlorine.Checked = False ' here is my checkbox
If ds.Tables(0).Rows.Count = 0 Then
' nothing for
Else
With ds.Tables(0).Rows(0)
If .Item("Confined") Then ' if I said .item("confined")=1, it would never work
chkConfined.Checked = True
End If
End With
End If