How to change boolean value in SQL Server query?

Basically I want to change the boolean value by selecting from the table:

eg:.

SELECT otherColumns, not ysnPending FROM table 

I need a column ysnPending = true if false and false if true.

Is there any function to change the boolean, or should I use IIf or CASE ...?

+6
sql sql-server tsql
source share
2 answers

use CASE , or if the bit is not zero, you can simply subtract from 1.

 SELECT otherColumns, (1 - ysnPending) -- NOT ysnPending FROM table 

(Using CASE can lead to clear code.)

If ysnPending is null, what behavior do you assign NOT ?

+3
source share

An example of using the case statement:

 create table table1 (id int not null, ysnPending bit null) insert table1 values (1, 1) insert table1 values (2, null) insert table1 values (3, 0) select id, cast((case when ysnPending = 1 then 0 else 1 end) as bit) as Not_ysnPending from table1 

It is assumed that you want to return 1 when ysnPending is NULL.

Cast to bit type โ€” Make sure the returned column is a BIT data type. If you leave it, it will return the INTEGER type. (This may or may not matter to you, depending on how you intend to use the returned result set).

0
source share

All Articles