Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return Bit Value as 1/0 and NOT True/False in SQL Server

Tags:

sql

select

bit

I have a Table in SQL Server 2000 with BitValue Column. But, it is being displayed as True/False in SQL Server Management Studio. When I do a Select * from Tablename it returns the BitValue Column values as True/False. How do I force it to return the value as bits (1/0) instead of True/False? Any Help will be really appreciated?

like image 505
Butters Avatar asked Jul 03 '13 14:07

Butters


People also ask

How do I return a bit value in SQL Server?

We need to directly return the calculated BIT value based on condition. So we use the CAST() method available in SQL. We can use the CAST method to cast from one data type to another.

What is bit value for false?

The string values TRUE and FALSE can be converted to bit values: TRUE is converted to 1 and FALSE is converted to 0. Converting to bit promotes any nonzero value to 1.

Is 1 true or false in SQL?

A Boolean table column will contain either string values of "True" and "False" or the numeric equivalent representation, with 0 being false and 1 being true.

How set True False in SQL Server?

Sql server does not expose a boolean data type which can be used in queries. Instead, it has a bit data type where the possible values are 0 or 1 . So to answer your question, you should use 1 to indicate a true value, 0 to indicate a false value, or null to indicate an unknown value.


3 Answers

Try with this script, maybe will be useful:

SELECT CAST('TRUE' as bit) -- RETURN 1
SELECT CAST('FALSE' as bit) --RETURN 0

Anyway I always would use a value of 1 or 0 (not TRUE or FALSE). Following your example, the update script would be:

Update Table Set BitField=CAST('TRUE' as bit) Where ID=1
like image 145
Gaston Flores Avatar answered Oct 10 '22 13:10

Gaston Flores


Modify your query to generate the output that you want.

Try casting them to int:

select cast(bitFlag as int)

Or, if you like, use case:

select (case when bitFlag = 0 then 0 else 1 end)
like image 21
Gordon Linoff Avatar answered Oct 10 '22 12:10

Gordon Linoff


This can be changed to 0/1 through using CASE WHEN like this example:

SELECT 
 CASE WHEN SchemaName.TableName.BitFieldName = 'true' THEN 1 ELSE 0 END AS 'bit Value' 
 FROM SchemaName.TableName
like image 6
Ashraf Sada Avatar answered Oct 10 '22 12:10

Ashraf Sada