How to check if bit variable is true or false in sql server?
sql-server
Solution
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.
Problem
I have a simple query and all I want to do is check if this variable is true or false, and for some reason it always returns false. ``` DECLARE @CappedIFCheck BIT SET @CappedIFCheck = (SELECT distinct 1 FROM mytable WHERE 1=1); select @CappedIFCheck IF (@CappedIFCheck = 'True') BEGIN SELECT 'true'; END ELSE BEGIN SELECT 'false'; END ```