4

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.

1 Answer 1

12

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;
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for pointing me a clear approach to this! I'll need to get a better grasp of sqlite's capabilities...

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.