Skip to content

An enhanced version of TO_BINARY that converts an input expression to a binary value, returning NULL if the conversion fails instead of raising an error.

See also: TO_BINARY

func.try_to_binary( <expr> )
func.try_to_binary('Databend')
┌───────────────────────────────────────┐
│ func.try_to_binary('Databend') │
├───────────────────────────────────────┤
4461746162656E64
└───────────────────────────────────────┘
TRY_TO_BINARY( <expr> )

This example successfully converts the JSON data to binary:

SELECT TRY_TO_BINARY(PARSE_JSON('{"key":"value", "number":123}')) AS binary_variant_success;
┌──────────────────────────────────────────────────────────────────────────┐
│ binary_variant │
├──────────────────────────────────────────────────────────────────────────┤
│ 40000002100000031000000610000005200000026B65796E756D62657276616C7565507B │
└──────────────────────────────────────────────────────────────────────────┘

This example demonstrates that the function fails to convert when the input is NULL:

SELECT TRY_TO_BINARY(PARSE_JSON(NULL)) AS binary_variant_invalid_json;
┌─────────────────────────────┐
│ binary_variant_invalid_json │
├─────────────────────────────┤
NULL
└─────────────────────────────┘