Purpose
This function is used to extract the value corresponding to a specific key from a key-value pair string.
Note
This function is supported starting from OceanBase Database V4.3.5 BP1.
Limitations and considerations
You cannot specify the same string for both
delimiter_1anddelimiter_2.If the string obtained by splitting with
delimiter_1contains multipledelimiter_2separators, the first occurrence ofdelimiter_2is used as the separator to obtain the corresponding key and value. For example, executingSELECT KEYVALUE('name:Li:xiang;age:12', ';', ':', 'name');returns the following result:+----------------------------------------------------+ | KEYVALUE('name:Li:xiang;age:12', ';', ':', 'name') | +----------------------------------------------------+ | Li:xiang | +----------------------------------------------------+ 1 row in set
Syntax
KEYVALUE(str, [delimiter_1, delimiter_2,] key)
Parameters
str: the string containing multiple key-value pairs. Supported types:VARCHAR,TINYTEXT,TEXT,LONGTEXT, andNULL.JSONis not supported.delimiter_1, delimiter_2,(optional): the separators. Details are as follows:delimiter_1: the separator for key-value pairs. Default value:;. Supported types:VARCHARandNULL.delimiter_2: the separator for keys and values. Default value::. Supported types:VARCHARandNULL.
key: the key to be searched for (key). Supported types:VARCHARandNULL. Thestrstring is split bydelimiter_1anddelimiter_2, and the value corresponding to thekeyis returned.
Return type
- If any parameter in the expression is
NULL, the result isNULL. - If the corresponding key is not found, the function returns
NULL. - If multiple key-value pairs match, the value corresponding to the first matching
keyis returned.
Examples
SELECT
KEYVALUE(NULL, ',', '=', 'b') AS key_value_null,
KEYVALUE('a=1,b=2,c=3', NULL, '=', 'b') AS delimiter_1_null,
KEYVALUE('a=1,b=2,c=3', ',', NULL, 'b') AS delimiter_2_null,
KEYVALUE('a=1,b=2,c=3', ',', '=', NULL) AS key_null,
KEYVALUE('a=1,b=2,c=3', ',', '=', 'b') AS b,
KEYVALUE('a=1,b=2,c=3,b=4', ',', '=', 'b') AS bs,
KEYVALUE('a:1;b:2;c:3', 'b') split_default;
The return result is as follows:
+----------------+------------------+------------------+----------+------+------+---------------+
| key_value_null | delimiter_1_null | delimiter_2_null | key_null | b | bs | split_default |
+----------------+------------------+------------------+----------+------+------+---------------+
| NULL | NULL | NULL | NULL | 2 | 2 | 2 |
+----------------+------------------+------------------+----------+------+------+---------------+
1 row in set