👋 Next.js Conf 2024: Come build, party, run, and connect with us! See all events

How to concatenate strings in GROQ and handle null values using coalesce function.

2 replies
Last updated: Aug 15, 2023
Concat string with GROQ

{
 firstName: "A",
 lastName: "B",
 fullName: firstName + lastName
}

// output: fullName: A B 
However, if one of the value is null, the output is null

{
 firstName: NULL,
 lastName: "B",
 fullName: firstName + lastName
}

//output: NULL
Expected: output: B
How should I check the Null value?
Aug 15, 2023, 12:08 PM
Perhaps you can try using
coalesce
function ?
coalesce(firstname, lastname)
Aug 15, 2023, 5:03 PM
Maybe try using
coalesce()
function ?
'fullName': coalesce(firstName, "") + coalesce(lastName,"")
Aug 15, 2023, 5:10 PM

Sanity– build remarkable experiences at scale

Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?