Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if bit variable is true or false in sql server?

Tags:

sql-server

People also ask

What bit value is true in SQL?

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.

How can check true or false in SQL Server?

SQL Server IIF() Function The IIF() function returns a value if a condition is TRUE, or another value if a condition is FALSE.

How do I select a bit value in SQL?

You can use the CONVERT operator. CAST or CONVERT will work. Show activity on this post. 1 is the display for a true bit.

What is the bit datatype in SQL Server?

SQL Server BIT data type is an integer data type that can take a value of 0, 1, or NULL . SQL Server optimizes storage of BIT columns. If a table has 8 or fewer bit columns, SQL Server stores them as 1 byte. If a table has 9 up to 16 bit columns, SQL Server stores them as 2 bytes, and so on.


A bit variable in SQL Server can have three values. 0, 1 and NULL.

The strings 'true' and 'false' map to 1 and 0 respectively.

Your code does not take account of the third possible value. If mytable is empty then the variable will not be initialised and have the value NULL.

SELECT CASE @CappedIFCheck
         WHEN 'True' THEN 'true'
         WHEN 'False' THEN 'false'
         ELSE 'unknown'
       END 

I'm not sure exactly what your code is trying to do but that is a very inefficient way of going about things. You should use EXISTS instead.


When comparing BIT values in Sql Server, use literal values 1 and 0 instead of 'True' and 'False'.

IF (@CappedIFCheck = 1) ...