linkyndy
linkyndy

Reputation: 17900

Conditional value in sqlite column

I have two integer columns in my sqlite table: a and b. I need to create a third column, c, which should contain either Y if a+b mod 2 == 1 or N if the previous condition is not satisfied. I am not sure how to define such a column with a conditional value in my query.

Upvotes: 4

Views: 3882

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269753

You can do this readily in a query:

select a, b, (case when (a + b) % 2 = 1 then 'Y' else 'N' end) as col3
from table t;

You can do this in an update statement as well:

update t
    set col3 = (case when (a + b) % 2 = 1 then 'Y' else 'N' end) ;

You need to be sure that col3 exists. You can do that with alter table:

alter table t add column col3 int;

Upvotes: 12

Related Questions