v.21.11New Features

Add Functions for User-Defined Default Values on String Parsing Failures

Add Function toUUIDOrDefault, toUInt8/16/32/64/256OrDefault, toInt8/16/32/64/128/256OrDefault, which enables user defining default value(not null) when string parsing is failed. #21330 (taiyang-li).
Added new functions toUUIDOrDefault, toUInt8/16/32/64/256OrDefault, and toInt8/16/32/64/128/256OrDefault that convert strings to UUID or integer types with a user-defined default value returned when parsing fails.

Why it matters

These functions address the need to safely parse string values into UUID or various integer types, providing a fallback default value instead of returning NULL or causing an error when the conversion fails. This improves data robustness and simplifies handling of malformed or unexpected input.

How to use it

Use the new functions by passing the string to be converted and the desired default value as arguments. For example:

SELECT toInt32OrDefault('123', 0);
SELECT toUUIDOrDefault('invalid-uuid', generateUUIDv4());

If the string parsing fails, the function returns the specified default value instead of NULL or error.