title: Oracle - Update column only if source is not null
body: <p>I have an update statement that is dynamically being generated within a package.</p>
  
  <p>I need to update the <code>target_field2</code> only when <code>source_field2</code> is <code>NOT NULL</code>, if <code>NULL</code> then keep <code>target_field2</code> as is.</p>
  
  <p>Hoping there is an easy answer to this. This is the dynamic section in concern:</p>
  
  <pre><code>                 ELSIF cur_field_to_update.FIELD_NAME = 'SOURCE_FIELD2'
                   THEN
                      v_update_values := 
                          v_update_values
                          || ', CASE WHEN NVL('''
                          || data_cur.IND_TYPE
                          || ''', NULL) &lt;&gt; ''D'' THEN NULL ELSE TO_NUMBER(NVL('
                          || cur_field_to_update.FIELD_NAME
                          || ','
                          || TARGET_FIELD2
                          || ')) END';
  </code></pre>
  
  <p>Which ends up looking like..</p>
  
  <pre><code>UPDATE target_table 
  SET (target_field1,target_field2) = 
  (SELECT source_field1, 
  CASE WHEN NVL('A',NULL) &lt;&gt; 'A' 
  THEN NULL   
  ELSE TO_NUMBER(NVL(source_field2,target_field2))
  END 
  FROM source_table);
  </code></pre>