'How to check if bit variable is true or false in sql server?
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
Solution 1:[1]
When comparing BIT values in Sql Server, use literal values 1 and 0 instead of 'True' and 'False'.
IF (@CappedIFCheck = 1) ...
Solution 2:[2]
Try the set clause like this:
SET @CappedIFCheck = ISNULL((select 1 where exists (select 1 from MyTable where 1=0)),0)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Jayvee |
