6

Basically I want to alter the boolean value selecting from the table:

e.g.:

SELECT otherColumns, not ysnPending FROM table

I need a column ysnPending = true if the value is false & false if the value is true.

Is there any function available to alter the Boolean value or I should use IIf or CASE...?

3
  • Please show your table schema. Commented Mar 6, 2010 at 6:02
  • ysnPending is of the type Bit Commented Mar 6, 2010 at 6:03
  • If ysnPending is nullable then you can't NOT it. NOT NULL is NULL Commented Mar 6, 2010 at 6:07

2 Answers 2

3

use CASE, or if the bit field is non-nullable you could just subtract from 1.

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

(Using CASE might lead to more understandable code.)

If ysnPending is nullable, what behaviour do you assign to NOT?

Sign up to request clarification or add additional context in comments.

3 Comments

SELECT otherColumns, CASE WHEN ysnPending = NULL THEN 1 WHEN ysnPending = 0 THEN 1 WHEN ysnPending = 1 THEN 0 End FROM table worked for me!
NULL maps to True? OK if that's what you need.
@Vikas - you probably want to use : "WHEN ysnPending IS NULL" instead of "WHEN ysnPending = NULL".
0

Example using a 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

Assumes you want 1 returned when ysnPending is NULL.

The cast to bit type is to make sure that the returned column is of a BIT datatype. If you leave it out, it will return an INTEGER type. (This may or may not matter to you, depending on how exactly you are going to use the returned result set).

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.