How do I only update null values in a column with SOQL?

1.4k views Asked by At

I have an issue with SOQL within a query activity in Salesforce Marketing Cloud Automation Studio.

I want to update null values with birth dates of users in a column named "date of birth" that includes birth dates of a ton of users with datatype = date.

I want to update this column, "date of birth", based on another column named "date of birth 2" which also includes birth dates of a ton of users but instead with datatype = string.

Thus, my goal is to be able to update only the missing date fields in the column "date of birth" leveraging data from the column "date of birth 2".

With my current very simple code (see img 1/2), I either update nothing in the data extension (when the WHERE column = NULL is added, see img 2) or I update the null values but also overwrite the existing date values in the data extension (when there's not added a WHERE column = NULL, see img 1). I wonder if it possible to simply update the null fields and let the currently filled fields stay?

Looking forward to hearing from you and please tell me if my query is unclear!

1

There are 1 answers

1
Asheesh Sahu On

So I think you can query on the object where your (date of birth) field is equal to null and (date of birth 2) is not equal null.

SELECT (date of birth),(date of birth 2) FROM objectName WHERE (date of birth) = NULL AND (date of birth 2) != NULL

Please make sure your (date of birth 2) field must be in YYYY-MM-DD format.

Sample code.

List<objectName> li = [SELECT (date of birth),(date of birth 2) FROM objectName WHERE (date of birth) = NULL AND (date of birth 2) != NULL];
for(objectName o : li){
 (date of birth) = Date.valueOf((date of birth2));
}
update li;