MySQL - UCASE() Function



The MySQL UCASE() function is used to convert a string value to all upper case letters.

It is equivalent to the UPPER() function in MySQL. This function can be useful in various scenarios such as formatting text for consistent comparisons, normalization, etc.

Syntax

Following is the syntax of MySQL UCASE() function −

UCASE(str)

Parameters

This function takes a string value as a parameter.

Return Value

This function returns the uppercase version of the given string.

Example

In the following example, we are converting the string 'tutorialspoint' to all uppercase letters −

SELECT UCASE('tutorialspoint');

Following is the output of the above code −

UCASE('tutorialspoint')
TUTORIALSPOINT

Example

To change the case of a binary string from lower to upper, you first need to convert it into a non-binary string and then proceed −

SELECT UCASE(CONVERT(BINARY 'tutorialspoint' USING utf8mb4));

The output obtained is as follows −

UCASE(CONVERT(BINARY 'tutorialspoint' USING utf8mb4))
TUTORIALSPOINT

Example

If the argument passed to the UCASE() function is NULL, it returns NULL −

SELECT UCASE(NULL);

We get the output as follows −

UCASE(NULL)
0x

Example

If you pass a numerical value to this function, it returns the same value −

SELECT UCASE(55886);

The result produced is as shown below −

UCASE(55886)
55886

Example

You can also convert the lower case letters in the column of a table to upper case using the UCASE() function.

Let us create a table named "EMP" and insert records into it using CREATE and INSERT statements as shown below −

CREATE TABLE EMP(
   FIRST_NAME  CHAR(20) NOT NULL,
   LAST_NAME  CHAR(20),
   AGE INT,
   INCOME FLOAT
);

Now, let us insert records into it using the INSERT statement −

INSERT INTO EMP VALUES 
('Krishna', 'Sharma', 19, 2000),
('Raj', 'Kandukuri', 20, 7000),
('Ramya', 'Ramapriya', 25, 5000),
('Mac', 'Mohan', 26, 2000);

The EMP obtained is as follows −

FIRST_NAME LAST_NAME AGE INCOME
Krishna Sharma 19 2000
Raj Kandukuri 20 7000
Ramya Ramapriya 25 5000
Mac Mohan 26 2000

Following query converts all the characters in the column 'FIRST_NAME' using the UCASE() function −

SELECT FIRST_NAME, LAST_NAME, AGE, UCASE(FIRST_NAME) as RESULT 
FROM EMP;

After executing the above code, we get the following output −

FIRST_NAME LAST_NAME AGE RESULT
Krishna Sharma 19 KRISHNA
Raj Kandukuri 20 RAJ
Ramya Ramapriya 25 RAMYA
Mac Mohan 26 MAC
mysql-ucase-function.htm
Advertisements