Collections:
WEIGHT_STRING() - String Sorting Weight
How to calculate the sorting weight of a given string using the WEIGHT_STRING() function?
✍: FYIcenter.com
WEIGHT_STRING(str AS type) is a MySQL built-in function that
calculates the sorting weight of a given string.
Output can be converted to a given data type.
For example:
SELECT WEIGHT_STRING('abc'), HEX(WEIGHT_STRING('abc'));
-- +----------------------+---------------------------+
-- | WEIGHT_STRING('abc') | HEX(WEIGHT_STRING('abc')) |
-- +----------------------+---------------------------+
-- | A B C | 004100420043 |
-- +----------------------+---------------------------+
SELECT WEIGHT_STRING('ABC'), HEX(WEIGHT_STRING('ABC'));
-- +----------------------+---------------------------+
-- | WEIGHT_STRING('ABC') | HEX(WEIGHT_STRING('ABC')) |
-- +----------------------+---------------------------+
-- | A B C | 004100420043 |
-- +----------------------+---------------------------+
SELECT 'ABC'='abc';
-- +-------------+
-- | 'ABC'='abc' |
-- +-------------+
-- | 1 |
-- +-------------+
SELECT HEX(WEIGHT_STRING('ABC' AS CHAR(5))), HEX(WEIGHT_STRING('ABC' AS BINARY(10)));
-- +--------------------------------------+-----------------------------------------+
-- | HEX(WEIGHT_STRING('ABC' AS CHAR(5))) | HEX(WEIGHT_STRING('ABC' AS BINARY(10))) |
-- +--------------------------------------+-----------------------------------------+
-- | 00410042004300200020 | 41424300000000000000 |
-- +--------------------------------------+-----------------------------------------+
Reference information of the WEIGHT_STRING() function:
WEIGHT_STRING(str AS type LEVEL levels): weight
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.
Arguments, return value and availability:
str: Required. The input value to be weighted.
AS type: Optional. Default is the same type as the input.
weight: Return value. The weight value of the input.
Available since MySQL 5.7.
Change history:
MySQL 8 - Removed the "LEVEL levels" clause, which allows you to control
the bit order of the weight string at character collation level.
⇒ MySQL Functions on Numeric Values
⇐ UPPER() - Convert String to Upper Case
2023-11-18, 1269🔥, 0💬
Popular Posts:
How To Create a Dynamic Cursor with the DYNAMIC Option in SQL Server Transact-SQL? If the underlying...
How To Replace Given Values with NULL using NULLIF() in SQL Server Transact-SQL? Sometime you want t...
How to calculate the storage size of a JSON (JavaScript Object Notation) value using the JSON_STORAG...
How REAL and FLOAT Literal Values Are Rounded in SQL Server Transact-SQL? By definition, FLOAT(n) sh...
Where to find answers to frequently asked questions on Downloading and Installing SQL Server 2005 Ex...