Categories:

String & binary functions (Encoding/Decoding)

TRY_HEX_DECODE_STRING¶

A special version of HEX_DECODE_STRING that returns a NULL value if an error occurs during decoding.

Syntax¶

TRY_HEX_DECODE_STRING(<input>)
Copy

Arguments¶

input

A hex-encoded string expression. Typically the input was created by a call to HEX_ENCODE.

Returns¶

The returned value is a string (VARCHAR).

Examples¶

This shows how to use the function:

Create a table and data:

CREATE TABLE hex (v VARCHAR, hex_string VARCHAR, garbage VARCHAR);
INSERT INTO hex (v, hex_string, garbage) 
  SELECT 'AaBb', HEX_ENCODE('AaBb'), '127';
Copy

Now run the query:

SELECT v, hex_string, TRY_HEX_DECODE_STRING(hex_string), TRY_HEX_DECODE_STRING(garbage) FROM hex;
Copy

Output:

+------+------------+-----------------------------------+--------------------------------+
| V    | HEX_STRING | TRY_HEX_DECODE_STRING(HEX_STRING) | TRY_HEX_DECODE_STRING(GARBAGE) |
|------+------------+-----------------------------------+--------------------------------|
| AaBb | 41614262   | AaBb                              | NULL                           |
+------+------------+-----------------------------------+--------------------------------+
Copy