Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>One way to accomplish this is to use an expression in the SQL statement that tests whether the supplied value is an empty string. If the supplied value is an empty string, then use the current value of the column as the value to assign to the column. Otherwise, assign the supplied value to the column.</p> <p>In the example below, the each of the supplied values have to be include TWICE in the statement: once in the conditional test, and then again, as a possible result of the conditional test.</p> <p>This statement:</p> <pre><code>UPDATE test_accs SET acc_owner = IF('2'='',acc_owner,'2') , acc_policy_version = IF('1.2'='',acc_policy_version,'1.2') , acc_policy_last_update = IF('2012-12-19'='',acc_policy_last_update,'2012-12-19') , acc_policy_next_update = IF('2012-12-18'='',acc_policy_next_update,'2012-12-18') WHERE acc_id = '1' </code></pre> <p>is equivalent to the first UPDATE statement in the question, in that it sets the value of all four columns to the new specified value.</p> <p>This statement:</p> <pre><code>UPDATE test_accs SET acc_owner = IF(''='',acc_owner,'') , acc_policy_version = IF('1.2'='',acc_policy_version,'1.2') , acc_policy_last_update = IF(''='',acc_policy_last_update,'') , acc_policy_next_update = IF(''='',acc_policy_next_update,'') WHERE acc_id = '1' </code></pre> <p>changes ONLY the value of the <code>acc_policy_version</code> column, the values of the other three columns will remain unchanged.</p> <p>This is not necessarily the best approach, but it is workable for some scenarios.</p> <p>It's also possible to create an expression that requires each supplied value be specified in the statement one time, although I think these expressions are a little less intuitive:</p> <pre><code> SET acc_owner = COALESCE(NULLIF( '' ,''),acc_owner ) , acc_policy_version = COALESCE(NULLIF( '1.2' ,''),acc_policy_version) </code></pre> <p>That's essentially doing the same thing as the examples above. </p> <p>If the supplied value is equal to '' (like it is for <code>acc_owner</code> in the example above), then the NULLIF expression will return a NULL. The COALESCE function essentially causes that NULL value to be skipped, and the current value of the column will remain unchanged (the current value of the column is assigned to the column.)</p> <p>If the supplied value is not equal to '' (like it is for <code>acc_policy_version</code> in the example above), then the NULLIF expression will return the supplied value. The COALESCE function will pick up that value, and assign it to the column.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload