MS SQL - BOOLEAN
[Link] KUMAR B.E.,MBA(HRM) 1
MS SQL - BOOLEAN
• A Boolean is a universal data type which stores true
or false values.
• It is used when we define a variable in a column of the
table.
• In MS SQL Server, there is no direct BOOLEAN data
type.
• Instead, you can use the BIT data type to represent
Boolean values, where 0 represents FALSE and 1
represents TRUE.
• The BIT data type can also accept NULL values.
[Link] KUMAR B.E.,MBA(HRM) 2
Example
declare @myBoolean bit
set @myBoolean='true'
select @myBoolean result
[Link] KUMAR B.E.,MBA(HRM) 3
declare @myBoolean bit
set @myBoolean='false'
select @myBoolean result
[Link] KUMAR B.E.,MBA(HRM) 4
Boolean variable to 1 instead of a true
value. This is a best practice.
declare @myBoolean bit
set @myBoolean=1
select @myBoolean result
[Link] KUMAR B.E.,MBA(HRM) 5
For a false value, you will set the value to 0.
declare @myBoolean bit
set @myBoolean=0
select @myBoolean result
[Link] KUMAR B.E.,MBA(HRM) 6
Set the myBoolean bit variable to null.
declare @myBoolean bit
set @myBoolean=NULL
select @myBoolean result
[Link] KUMAR B.E.,MBA(HRM) 7
How to create a table with a bit data type column
create table myBooleanTable
(
id int,
ispair bit
)
select * from myBooleanTable
[Link] KUMAR B.E.,MBA(HRM) 8
How to insert data with SQL Boolean data type
insert into myBooleanTable values
(1,0),
(2,1),
(5,NULL)
select * from myBooleanTable
[Link] KUMAR B.E.,MBA(HRM) 9