How to Use the UPPER() Function in MySQL

08/05/2021

Contents

In this article, you will learn how to use the UPPER() function in MySQL.

Using the UPPER() function in MySQL

In MySQL, the UPPER() function is used to convert all characters in a string to uppercase.

Syntax

UPPER(str);

Examples

Here are some examples of using the UPPER() function in MySQL:

Converting a string to uppercase

To convert a string to uppercase, you can use the UPPER() function.

Syntax
SELECT UPPER(str);
Example
SELECT UPPER('hello world');

Output:

+-----------------+
| UPPER('hello world')|
+-----------------+
| HELLO WORLD     |
+-----------------+

In this example, the UPPER() function returns the string ‘HELLO WORLD’, which is the uppercase version of the input string ‘hello world’.

Converting column values to uppercase

You can also use the UPPER() function to convert column values to uppercase.

Syntax
SELECT UPPER(column_name) FROM table_name;
Example
SELECT UPPER(name) FROM employees;

Output:

+--------+
| UPPER(name)|
+--------+
| JOHN   |
| ANN    |
| RICH   |
| SARA   |
+--------+

In this example, the UPPER() function returns the uppercase version of the name column in the employees table.

Using uppercase values in a WHERE clause

You can use the UPPER() function in a WHERE clause to filter records based on uppercase values.

Syntax
SELECT * FROM table_name WHERE UPPER(column_name) = string_value;
Example
SELECT * FROM employees WHERE UPPER(name) = 'JOHN';

Output:

+----+------+--------+------------+
| id | name | salary | hire_date  |
+----+------+--------+------------+
| 1  | John | 50000  | 2020-01-01 |
+----+------+--------+------------+

In this example, the SELECT statement returns all the records from the employees table where the name column is equal to ‘JOHN’ in uppercase.