Search This Blog

Tuesday, September 10, 2013

Bit Data Type SQL Server

SQL Server > Data Types > Bit

Bit is an integer data type that can take a value of 1, 0, or NULL.
 
Example: Use bit data type to set user is admin or not


CREATE TABLE #Users(
  [Name]  [nvarchar](50) NOT NULL,
  [Admin] [bit] NOT NULL
)

ALTER TABLE [#Users] ADD CONSTRAINT [DF_Users_Admin] DEFAULT ((0)) FOR [Admin]
GO

insert into #Users(Name, Admin) values ('Admin',1), ('John',0)

select * from #Users where Admin = 1
drop table #Users

Result:

Name   Admin
Admin  1