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
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.
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.
For example, MySQL supports SELECT FOR UPDATE, but SQLite does not.
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.
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With