Extracts the value of a specified key from a key-value pair string. The function splits the input string into key-value pairs using split1, separates keys from values using split2, and returns the value that matches the given key.
Syntax
KEYVALUE(STRING <str>, [STRING <split1>, STRING <split2>,] STRING <key>)
KEYVALUE(STRING <str>, STRING <key>)Parameters
Parameter | Required | Description |
str | Yes | The input string to parse. |
split1 | No | The delimiter that separates key-value pairs. Defaults to a semicolon (;). |
split2 | No | The delimiter that separates keys from values within each pair. Defaults to a colon (:). |
key | Yes | The key whose corresponding value is returned. |
If a key-value pair (after splitting by split1) contains multiple occurrences of split2, the return value is undefined for that pair.
Return value
Returns a value of the STRING type. The return value follows these rules:
Returns
nullifsplit1orsplit2isnull.Returns
nullifstrorkeyisnull, or if no matching key is found.Returns the value of the first matching key if multiple key-value pairs share the same key.
Examples
Use default delimiters
Split 0:1\;1:2 and return the value for key 1. Because split1 and split2 are not specified, the function uses the defaults: semicolon (;) as the pair delimiter and colon (:) as the key-value delimiter.
-- Returns 2.
SELECT KEYVALUE('0:1\;1:2', 1);After splitting by ;, the key-value pairs are:
0 1\
1 2The value for key 1 is 2.
Use custom delimiters
Split \;decreaseStore:1\;xcard:1\;isB2C:1\;tf:21910\;cart:1\;shipping:2\;pf:0\;market:shoes\;instPayAmount:0\; using \; as the pair delimiter and : as the key-value delimiter, then return the value for key tf.
-- Returns 21910.
SELECT KEYVALUE("\;decreaseStore:1\;xcard:1\;isB2C:1\;tf:21910\;cart:1\;shipping:2\;pf:0\;market:shoes\;instPayAmount:0\;","\;",":","tf");After splitting by \;, the key-value pairs are:
decreaseStore 1
xcard 1
isB2C 1
tf 21910
cart 1
shipping 2
pf 0
market shoes
instPayAmount 0The value for key tf is 21910.
Related functions
KEYVALUE is a string function. For a full list of string search and conversion functions, see String functions.