πŸ“© 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 TotalPurchase
FROM 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 GreetingName
FROM 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.

Read more