Converts a BINARY value to a STRING. If the input contains invalid UTF-8 byte sequences, the function returns NULL instead of raising an error.
Syntax
STRING SAFE_CONVERT_BYTES_TO_STRING(BINARY <value>)Parameters
| Parameter | Required | Type | Description |
|---|---|---|---|
value | Yes | BINARY | The binary value to convert. If the value contains invalid UTF-8 byte sequences, the function returns NULL. Passing NULL raises an error. |
Return value
Returns a STRING. The following rules apply:
If the input is a STRING, the original string is returned unchanged.
If the input is NULL, an error is returned.
If the input contains invalid UTF-8 byte sequences, NULL is returned.
Examples
-- Cast a string literal to BINARY, then convert back to STRING
-- Returns: alibaba
SELECT SAFE_CONVERT_BYTES_TO_STRING(CAST('alibaba' AS BINARY));
-- Convert a hexadecimal literal to STRING
-- Returns: abc
SELECT SAFE_CONVERT_BYTES_TO_STRING(X'616263');
-- Convert the result of UNHEX to STRING
-- Returns: abc
SELECT SAFE_CONVERT_BYTES_TO_STRING(UNHEX('616263'));
-- Pass a string literal directly
-- Returns: 010101
SELECT SAFE_CONVERT_BYTES_TO_STRING('010101');
-- NULL input raises an error
-- Error: ODPS-0130071:[1,8] Semantic analysis exception - function SAFE_CONVERT_BYTES_TO_STRING is ambiguous with (VOID), candidates are STRING SAFE_CONVERT_BYTES_TO_STRING(BINARY arg0); STRING SAFE_CONVERT_BYTES_TO_STRING(STRING arg0)
SELECT SAFE_CONVERT_BYTES_TO_STRING(null);
-- Invalid UTF-8 byte sequence returns NULL
SELECT SAFE_CONVERT_BYTES_TO_STRING(UNHEX('\xc2'));Related functions
SAFE_CONVERT_BYTES_TO_STRING is a string function. For other functions that search strings or convert string formats, see String functions.