Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update Command with Case in SQLite

I want to update table A in such a way that if the attribute of the table column is desired then only it will change otherwise it wont change..

Update table A set B="abcd" ,C= (case when C="abc" then C="abcd" else C end) where column =1;

means C should be only change when in column=1 and C value is abc otherwise C should not be update ..it should be dropped and only B changes. but if the C value get matched i.e abc give me the output 0 .. not changing to the abcd

like image 614
Hitesh Avatar asked Jun 13 '13 05:06

Hitesh


People also ask

Does SQLite support case statement?

SQLite has a case statement which allows us to include conditional logic in our SQL statements. SQLite case expressions execute a list of conditions and return an expression based on the results. SQLite case statements behave identically to IF – THEN – ELSE statements like the other scripting languages.

How do I update multiple columns in SQLite?

First, specify the table where you want to update after the UPDATE clause. Second, set new value for each column of the table in the SET clause. Third, specify rows to update using a condition in the WHERE clause. The WHERE clause is optional.

Does SQLite support select for update?

For example, MySQL supports SELECT FOR UPDATE, but SQLite does not.

What is update in SQLite?

SQLite UPDATE Query is used to modify the existing records in a table. You can use WHERE clause with UPDATE query to update selected rows, otherwise all the rows would be updated.


1 Answers

Inside the THEN part, C="abcd" compares C with the value, and returns either 1 or 0.

The entire CASE expression should just return a value that then gets written into the C column, so you want just 'abcd' in this place:

UPDATE tableA
SET B = 'abcd',
    C = CASE
        WHEN C = 'abc' THEN 'abcd'
                       ELSE C
        END
WHERE column = 1;
like image 143
CL. Avatar answered Sep 16 '22 15:09

CL.