Collections:
CONVERT() - Character Set Conversion
How to convert a character string into a given character set using the CONVERT() function?
✍: FYIcenter.com
CONVERT(str USING charset) is a MySQL built-in function that
converts a character string into a given character set.
For example:
SET @str = 'FYICenter'; SELECT @str, LENGTH(@str); -- +-----------+--------------+ -- | @str | LENGTH(@str) | -- +-----------+--------------+ -- | FYICenter | 9 | -- +-----------+--------------+ SELECT CONVERT(@str USING utf8mb4), LENGTH(CONVERT(@str USING utf8mb4)); -- +-----------------------------+-------------------------------------+ -- | CONVERT(@str USING utf8mb4) | LENGTH(CONVERT(@str USING utf8mb4)) | -- +-----------------------------+-------------------------------------+ -- | FYICenter | 9 | -- +-----------------------------+-------------------------------------+
Note that the CONVERT() function has another syntax version CONVERT(exp, type) to perform data type conversion, which is equivalent to the CAST() function. For example,
SET @exp = '-1.99 USD'; SELECT @exp, CONVERT(@exp, SIGNED INTEGER), CONVERT(@exp, UNSIGNED INTEGER); -- +-----------+-------------------------------+---------------------------------+ -- | @exp | CONVERT(@exp, SIGNED INTEGER) | CONVERT(@exp, UNSIGNED INTEGER) | -- +-----------+-------------------------------+---------------------------------+ -- | -1.00 USD | -1 | 18446744073709551615 | -- +-----------+-------------------------------+---------------------------------+ SELECT @exp, CAST(@exp AS SIGNED INTEGER), CAST(@exp AS UNSIGNED INTEGER); -- +-----------+------------------------------+--------------------------------+ -- | @exp | CAST(@exp AS SIGNED INTEGER) | CAST(@exp AS UNSIGNED INTEGER) | -- +-----------+------------------------------+--------------------------------+ -- | -1.99 USD | -1 | 18446744073709551615 | -- +-----------+------------------------------+--------------------------------+
Reference information of the CONVERT() function:
CONVERT(str USING charset): newstr Converts a character string into a given character set. Arguments, return value and availability: str: Required. The character string to be converted. charset: Required. The character set to be converted to. newstr: Return value. The converted character string. Available since MySQL 4.0. CONVERT(exp, type): val Casts an expression to a value of the given data type. Arguments, return value and availability: exp: Required. The expression to be converted. type: Required. The data type to be converted to. val: Return value. The converted value of the given type. Available since MySQL 4.0.
Related MySQL functions:
⇒ DEFAULT() - Table Column Default Value
⇐ COALESCE() - Finding First Non-NULL Value
2023-12-19, 1024🔥, 0💬
Popular Posts:
How To Use SQL*Plus Built-in Timers in Oracle? If you don't have a stopwatch/timer and want to measu...
What Is an Oracle Instance in Oracle? Every running Oracle database is associated with an Oracle ins...
How To Connect ASP Pages to Oracle Servers in Oracle? If you are running Windows IIS Web server and ...
How To Use DATEADD() Function in SQL Server Transact-SQL? DATEADD() is a very useful function for ma...
How to execute statements under given conditions in SQL Server Transact-SQL? How to use IF ... ELSE ...