MySQL tutorial: WEIGHT_STRING [EN]
top of page
CerebroSQL

MySQL: 

WEIGHT_STRING

Syntax:
WEIGHT_STRING(str [AS {CHAR|BINARY}(N)] [flags])

This function returns the weight string for the input string. The
return value is a binary string that represents the comparison and
sorting value of the string. It has these properties:

o If WEIGHT_STRING(str1) = WEIGHT_STRING(str2), then str1 = str2 (str1
and str2 are considered equal)

o If WEIGHT_STRING(str1) < WEIGHT_STRING(str2), then str1 < str2 (str1
sorts before str2)

WEIGHT_STRING() is a debugging function intended for internal use. Its
behavior can change without notice between MySQL versions. It can be
used for testing and debugging of collations, especially if you are
adding a new collation. See
https://dev.mysql.com/doc/refman/8.0/en/adding-collation.html.

This list briefly summarizes the arguments. More details are given in
the discussion following the list.

o str: The input string expression.

o AS clause: Optional; cast the input string to a given type and
length.

o flags: Optional; unused.

The input string, str, is a string expression. If the input is a
nonbinary (character) string such as a CHAR, VARCHAR, or TEXT value,
the return value contains the collation weights for the string. If the
input is a binary (byte) string such as a BINARY, VARBINARY, or BLOB
value, the return value is the same as the input (the weight for each
byte in a binary string is the byte value). If the input is NULL,
WEIGHT_STRING() returns NULL.

Examples:

mysql> SET @s = _utf8mb4 'AB' COLLATE utf8mb4_0900_ai_ci;
mysql> SELECT @s, HEX(@s), HEX(WEIGHT_STRING(@s));
+------+---------+------------------------+
| @s | HEX(@s) | HEX(WEIGHT_STRING(@s)) |
+------+---------+------------------------+
| AB | 4142 | 1C471C60 |
+------+---------+------------------------+

mysql> SET @s = _utf8mb4 'ab' COLLATE utf8mb4_0900_ai_ci;
mysql> SELECT @s, HEX(@s), HEX(WEIGHT_STRING(@s));
+------+---------+------------------------+
| @s | HEX(@s) | HEX(WEIGHT_STRING(@s)) |
+------+---------+------------------------+
| ab | 6162 | 1C471C60 |
+------+---------+------------------------+

mysql> SET @s = CAST('AB' AS BINARY);
mysql> SELECT @s, HEX(@s), HEX(WEIGHT_STRING(@s));
+------+---------+------------------------+
| @s | HEX(@s) | HEX(WEIGHT_STRING(@s)) |
+------+---------+------------------------+
| AB | 4142 | 4142 |
+------+---------+------------------------+

mysql> SET @s = CAST('ab' AS BINARY);
mysql> SELECT @s, HEX(@s), HEX(WEIGHT_STRING(@s));
+------+---------+------------------------+
| @s | HEX(@s) | HEX(WEIGHT_STRING(@s)) |
+------+---------+------------------------+
| ab | 6162 | 6162 |
+------+---------+------------------------+

URL: https://dev.mysql.com/doc/refman/8.0/en/string-functions.html

Example

bottom of page