[SQL, SQL Server]
SQL Server, as you are no doubt aware, has a type that can be used to represent boolean states -
This type is called BIT
true is represented as
DECLARE @IsDaylight BIT = 1; DECLARE @IsNightTime BIT = 0; SELECT @IsDaylight IsDayLight, @IsNightTime IsNightTime;
If you run this it will print the following:
What you might not know is that you can use
strings to represent true and false values.
SET @IsDaylight = 'FALSE'; SET @IsNightTime = 'TRUE'; SELECT @IsDaylight IsDayLight, @IsNightTime IsNightTime;
The strings have to be the values ‘
True’ or ‘
False’ (not case sensitive). Any other values will give you an error.
I think this is easier to read than
Running the script will return the following:
Another interesting thing to note - any value that is not 0 is considered
true, including negative numbers.
SET @IsDaylight = 2 SELECT @IsDaylight IsDayLight; SET @IsDaylight = -1; SELECT @IsDaylight IsDayLight;
This script will print the following: