Tuesday 30 July 2019

How the UPPER() Function Works in MySQL

In MySQL, the UPPER() function converts lowercase characters to uppercase, according to the current character set mapping (the default mapping is utf8mb4).

Syntax

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

Example

Here’s an example:
SELECT UPPER('cat');
Result:
+--------------+
| UPPER('cat') |
+--------------+
| CAT          |
+--------------+
As you might expect, if the string already contains any uppercase characters, those characters will remain in uppercase.
Example:
SELECT UPPER('Cat');
Result:
+--------------+
| UPPER('Cat') |
+--------------+
| CAT          |
+--------------+

Database Example

Here’s an example of selecting data from a database and converting it to uppercase:
USE Music;
SELECT 
    ArtistName AS Original, 
    UPPER(ArtistName) AS Uppercase
FROM Artists
LIMIT 5;
Result:
+------------------+------------------+
| Original         | Uppercase        |
+------------------+------------------+
| Iron Maiden      | IRON MAIDEN      |
| AC/DC            | AC/DC            |
| Allan Holdsworth | ALLAN HOLDSWORTH |
| Buddy Rich       | BUDDY RICH       |
| Devin Townsend   | DEVIN TOWNSEND   |
+------------------+------------------+

Binary Strings

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

The UCASE() Function

The UCASE() function is a synonym for UPPER(). Note that if you use UCASE() within a view, it will be rewritten and stored as UPPER().

Convert to Lowercase

The LOWER() and LCASE() functions work the same way to convert characters to lowercase.

0 comments:

Post a Comment