Tuesday, 30 July 2019

How the UCASE() Function Works in MySQL

In MySQL, the UCASE() function is a synonym for UPPER(). In other words, you can use either of these functions to do the same thing.
Specifically, both functions convert lowercase characters to uppercase (and return the result), according to the current character set mapping (the default mapping is utf8mb4).

Syntax

The syntax goes like this:
UCASE(str)
Where str is the string to be changed to uppercase.

Example

Here’s an example:
SELECT 
  UCASE('cat'),
  UPPER('cat');
Result:
+--------------+--------------+
| UCASE('cat') | UPPER('cat') |
+--------------+--------------+
| CAT          | CAT          |
+--------------+--------------+

Binary Strings

These functions don’t work on binary strings. If you need to use them on a binary string, you’ll need to convert the string to a nonbinary string first. Here’s an example:
SET @str = BINARY 'Cat';
SELECT 
  UCASE(@str) AS 'Binary', 
  UCASE(CONVERT(@str USING utf8mb4)) AS 'Nonbinary';
Result:
+--------+-----------+
| Binary | Nonbinary |
+--------+-----------+
| Cat    | CAT       |
+--------+-----------+

Using UCASE() in Views

One thing to be aware of is that, when the UCASE() function is used within a view, it is rewritten and stored as UPPER().

0 comments:

Post a Comment