42

I have column type of INT, I want to select 1 if it contains 1 and 0 otherwise. I know only way to do it using CASE:

CASE WHEN val=1 THEN 1 ELSE 0

What other approaches there is to achieve the same result?

9
  • 2
    DECLARE @r VARCHAR(10)='1' SELECT CAST(@r AS BIT)
    – Royi Namir
    Dec 12, 2013 at 13:42
  • 1
    @RoyiNamir: INT but string ;) Dec 12, 2013 at 13:42
  • 1
    @danihp will work also if val is int :-)
    – Royi Namir
    Dec 12, 2013 at 13:43
  • 1
    @RoyiNamir ... just to catch OP question. Dec 12, 2013 at 13:44
  • 1
    You can try SELECT CAST(val AS bit)
    – Snorre
    Dec 12, 2013 at 13:44

4 Answers 4

53

SQL SERVER 2012:

SELECT CAST(IIF ( field = 1, 1, 0 ) AS BIT) FROM table

Otherwise:

SELECT CAST(CASE field WHEN 1 THEN 1 ELSE 0 END AS BIT) From table
2
  • 2
    I believe best practice is to check if field = 0 instead
    – Greg
    Dec 12, 2013 at 13:54
  • @Greg That wouldn't solve the OP's problem, which is specifically about testing for the value 1.
    – Stewart
    Feb 21, 2020 at 10:53
17

All you have to do is cast/convert the int to a bit.

DECLARE
     @val INT = 42;
    ,@bitVal BIT;

SET @bitVal = CAST(@val AS BIT); -- equals 1

Any non-zero number will be converted to true.

https://technet.microsoft.com/en-us/library/ms191530(v=sql.105).aspx#_bit

2
  • 1
    The OP's question says "select 1 if it contains 1 and 0 otherwise". So this would convert any value other than 0 or 1 to the wrong value.
    – Stewart
    Feb 21, 2020 at 10:55
  • You are correct. :) I read the title and apparently just skimmed the question.
    – phokus
    Feb 28, 2020 at 1:44
4

To convert is simple, as long as it is 1 or 0 you can just assign it, no need to cast anything

you do the same when you declare a variable right?

@declare @myBit bit =1;

however when you want to do something more useful with the integer like bitmap comparison things get more interesting. you can compare two integers and return a bit based on the way the defined.
Let me try and visualise

    00000001=1
    00000010=2
    00000011=3
==============
    FFFFFFTT

so if you have a 1 the value becomes True,

Actually it is a bitmap comparison with 2 values is like mapping a raster, you can have several or even group them using 0+1 at a byte level.

Above you can see that 1 "is in" 3 as well as that 2 "is in" 3

look at the TSQL unit test example below

declare @notSet     int =0
      , @CanView    int =1 
      , @CanEdit    int =2 
      , @CanSubmit  int =4 
      , @CanApprove int =8
      , @CanDelete  int =16; 

declare @contributor int = @CanView | @CanEdit | @CanSubmit     --> Can't delete
       , @moderator  int = @CanView | @CanEdit | @CanDelete     --> not allowed to sumbit
       , @admin      int = @CanView | @CanEdit | @CanSubmit | @CanApprove | @CanDelete;--> can do all

SELECT TEST='A admin can Submit'            , RESULT= iif(@admin     & @CanSubmit = @CanSubmit,'TRUE','FALSE')
UNION ALL
SELECT TEST='A Moderator may not Submit'    , RESULT= iif(@moderator & @CanSubmit = @CanSubmit,'FALSE','TRUE')
UNION ALL
SELECT TEST='A Contributer may not delete'  , RESULT= iif(@contributor & @CanDelete = @CanDelete,'FALSE','TRUE')
UNION ALL
SELECT TEST='A Moderator may delete'        , RESULT= iif(@moderator & @CanDelete = @CanDelete,'TRUE','FALSE')

You can combine the values together using the bit operator | like this 1|2 = 3, and 1|1 =1, do not mix up "|" with a "+" here as it will not always work well ;-)

An example, the bug would be @CanView + @CanView would be @CanEdit,

when you expect @CanView | @canView will still be @canView

Try it in SQL

SELECT (1|1), (1+1) 

Below some C#, Hope it helps those that like to save and work with enums in code and database.

Say you have an Enum and a class like this: [Flags] public enum Rights { notSet =0 , CanView =1 , CanEdit =2 , CanSubmit =4 , CanApprove =8 , CanDelete =16 }

public class User
{
  public Rights Permission {get;set}
}

...
// user can change his own posts
var user = new User();
user.Permission = Rights.CanView | Rights.CanEdit | Rights.CanDelete ;

You can now store the permission in the database and read the value using one of the below methods where you compare one or compare several

var result = user.Permission & Rights.CanView  == Rights.CanView;
var canChange = ((user.Permission & (Rights.CanView  | Rights.CanEdit | Rights.CanDelete)) != 0);

So, Round Tripped to the database and back, hope you have what you are looking for

Happy coding,

Walter

1
  • This would convert any value other than 0 or 1 to 1, rather than 0 as the OP is asking for.
    – Stewart
    Feb 21, 2020 at 11:06
1

Here is one more solution. Without case or iif:

select ~cast(sign(abs(@i - 1)) as bit)

But it doesn't work if @i = -2147483648. You should then convert it to bigint.

1
  • 1
    all you need is this: SELECT~cast(val-1as bit) Oct 5, 2020 at 13:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.