0

I have a database where the creator has used decided to use smallint instead of bit for a true/false scenario. False is represented with 0. True is represented with -1. I would like to translate this, preferably in a simpler syntax than CASE. Is there any way I can do something like this:

SELECT (column == -1) 'value_as_bool'

FROM   myTable

Which would (in most languages) translate the column back to a true/false column...

Jakob Busk Sørensen
  • 5,599
  • 7
  • 44
  • 96

4 Answers4

1

You can use IIF function. Usage; IIF ( boolean_expression, true_value, false_value )

Burak
  • 186
  • 9
1

In a Case, the statement would be:

Case when myTable.column = -1 then 0 else 1 end as [NAME]

You can replace 0 and 1 with FALSE and TRUE, which uses the BOOLEAN/bit type in SQL, if you wish :)

Martin
  • 63
  • 7
  • General syntax is case when [statement with boolean output] then [value if true] when [statement with boolean output] then [value if true] when ... else [value if all of the above is false] end (possibly with 'as [#NAME#]' afterwards – Martin May 11 '17 at 08:01
  • How do I control the output? If I use 0 and 1, then will it be output as a `bit` or as some sort of `integer`? – Jakob Busk Sørensen May 11 '17 at 08:08
  • you can control it by using the CONVERT function, for example use: CONVERT(smallint,1) , which converts 1 to a smallint. documentation of the funktion: https://www.w3schools.com/sqL/func_convert.asp – Martin May 11 '17 at 08:55
1

I think the simplest solution would be to just cast the int value to a bit. 0 will be False everything else True.

SELECT CAST(column As Bit) 'value_as_bool'
FROM   myTable
Magnus
  • 45,362
  • 8
  • 80
  • 118
  • I think you are right. I am just have to live with the performance impact. – Jakob Busk Sørensen May 11 '17 at 08:13
  • If you can change the schema you could always add a persisted computed column to your table and use that instead. You would not get any performance hit. – Magnus May 11 '17 at 08:15
  • Single quotes are string delimiters. It's double quotes for names in standard SQL and brackets in SQL Server AFAIK. But as `value_as_bool` already is a valid name you need no delimiters at all. – Thorsten Kettner May 11 '17 at 08:25
  • @ThorstenKettner it works with single quotes, but it also works without. I prefer to avoid double quotes, since the query is to be used in VBA, where double quotes will end the string... – Jakob Busk Sørensen May 11 '17 at 08:33
  • @Noceo: Better have valid names without any kind of quotes. Using single quotes for names is (according to standard SQL) invalid has its Limits. Just try: `SELECT 'value_as_bool' FROM (SELECT CAST(column As Bit) 'value_as_bool' FROM myTable) t`. – Thorsten Kettner May 11 '17 at 09:05
0

You can declare a variable a return it after set in a IF/ELSE Statement like :

DECLARE @Result Bit

IF column =-1  
    SET @Result = 0
ELSE
    SET @Result = 1
RETURN @Result

Here for more Info about bool values in SQL:

In Management Studio it displays as a false/true value (at least in recent versions).

When accessing the database through ASP.NET it will expose the field as a boolean value.

Hope this help you.

Community
  • 1
  • 1
Francois Borgies
  • 2,378
  • 31
  • 38