Showing posts with label Mysql UPPER. Show all posts
Showing posts with label Mysql UPPER. Show all posts

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.

How to Convert Lowercase Characters to Uppercase in MySQL

In MySQL, you can use the UPPER() function to convert any lowercase characters to uppercase. Alternatively, you can use the UCASE() function, which is a synonym for UPPER().
The syntax goes like this:
UPPER(str)
Or…
UCASE(str)
Where str is the string you want converted to uppercase.

Example

Here’s an example:
SELECT UPPER('homer');
Result:
+----------------+
| UPPER('homer') |
+----------------+
| HOMER          |
+----------------+
If the string already contains any uppercase characters, then they will remain uppercase.
Example:
SELECT UPPER('Homer');
Result:
+----------------+
| UPPER('Homer') |
+----------------+
| HOMER          |
+----------------+

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   |
+------------------+------------------+
The LOWER() and LCASE() functions work the same way to convert characters to lowercase.