Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditional value in sqlite column

Tags:

sql

sqlite

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.

like image 579
linkyndy Avatar asked Nov 15 '25 04:11

linkyndy


1 Answers

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;
like image 68
Gordon Linoff Avatar answered Nov 17 '25 19:11

Gordon Linoff



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!