
Grab your gear: The official Sanity swag store
Read Grab your gear: The official Sanity swag storeYes, absolutely! You can use the select() function in GROQ to handle conditional values. This is exactly what you need for your use case.
The select() function works like a switch statement and evaluates conditions in order, returning the first matching value. Here's how you'd solve your specific case:
*[_type == "yourType"] {
_id,
"date": select(
defined(customDate) => customDate,
_updatedAt
)
}This will:
customDate exists and has a valuecustomDate if it's defined_updatedAt if customDate is not setThe defined() function checks whether a field exists and has a non-null value, which is perfect for your scenario. You can also use multiple conditions if needed:
*[_type == "yourType"] {
_id,
"date": select(
defined(customDate) && customDate != "" => customDate,
defined(publishedDate) => publishedDate,
_updatedAt
)
}This approach ensures your date property always has a value directly in the query, exactly as you wanted. The select() function stops evaluating once it finds the first truthy condition, so it's efficient too.
Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.
Content operations
Content backend


The only platform powering content operations
By Industry


Tecovas strengthens their customer connections
Build and Share

Grab your gear: The official Sanity swag store
Read Grab your gear: The official Sanity swag store