I have been using a sql like this to update a list of properties in my database:
update my_table set a = ?, b = ?, c = ?, d = ?, where customer = ?
But I want to update a property with a new value ONLY if it does not have a value in the database already. How can I do that?
Output: Column value can also be set to NULL without specifying the 'where' condition.
The UPDATE statement in SQL is used to update records in the table. We can modify one or multiple records (rows) in a table using UPDATE statement. If you do not use WHERE clause in UPDATE statement, all the records in the table will be updated.
We can return 0 for NULL in MySQL with the help of IFNULL() method. The syntax of IFNULL() is as follows. IFNULL(YOUREXPRESSION,0);
In MS SQL something like this (assuming non value means database NULL
) should work:
update
my_table
set
a = COALESCE(a, ?),
b = COALESCE(b, ?),
c = COALESCE(c, ?),
d = COALESCE(d, ?)
where
customer = ?
COALESCE()
returns first non null value from its arguments.
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