π© Learning #4 - Salesforce Marketing Cloud -SQL Function "Coalesce"
π What is the COALESCE Function?
The COALESCE() function in SQL is used to handle NULL values - in simple terms, it replaces NULL with a fallback value that makes your data usable.
π Syntax (ref: w3schools.com):
SELECT COALESCE(val1, val2, ..., val_n)
- You can use text, numbers,
NULL, empty strings, etc. - It returns the first non-null value from left to right.
π§ͺ General Use Case: Summing Purchase Values
Letβs say a customer made 4 purchases but one value is NULL.
Without COALESCE, the sum will return NULL.
β Use it like this:
SELECT COALESCE(Purchase1, 0) + COALESCE(Purchase2, 0) + COALESCE(Purchase3, 0) + COALESCE(Purchase4, 0) AS TotalPurchaseFROM Orders
This ensures every NULL is treated as 0 - and totals calculate correctly.
π§© SFMC Use Case: First Name Fallback in Emails
When FirstName is missing, your email might look like:
"Hello ," π¬
β Fix it with:
SELECT
SubscriberKey, COALESCE(FirstName, 'Valued Customer') AS GreetingNameFROM Contacts
So the email shows:
"Hello Valued Customer" instead of a blank greeting.
I hope this quick tip helps!
If you have more examples or use this while prepping for a cert, drop them in the comments π
π Note: These are personal insights. Always cross-check with official SFMC docs for accuracy.
Let me know if you'd like it exported in markdown or HTML format too.