Convert Strings to Lowercase in MySQL

In MySQL, you can use LOWER() function to convert any string to lowercase, which can be applied to both static text and data stored in table columns.

Converting strings to lowercase is often helpful for ensuring consistent data formatting in SQL databases. For instance, lowercase strings are commonly used in email addresses, usernames, and case-insensitive searches.


Using the LOWER() Function in MySQL

The LOWER() function in MySQL takes a string input and returns that string with all characters converted to lowercase. The syntax for using LOWER() is straightforward:

</>
Copy
LOWER(string)

Here, string can be a literal text, a column value, or any expression that evaluates to a string. Let’s set up an example table and use the LOWER() function in several examples to demonstrate how it works.


Setting Up an Example Table

To demonstrate the LOWER() function, we’ll create a sample customers table with fields for username, email, and city. These fields will allow us to explore various use cases for converting text to lowercase.

1. Create a new database called app_data:

</>
Copy
CREATE DATABASE app_data;

2. Switch to the app_data database:

</>
Copy
USE app_data;

3. Create a customers table with fields username, email, and city:

</>
Copy
CREATE TABLE customers (
    id INT PRIMARY KEY AUTO_INCREMENT,
    username VARCHAR(50),
    email VARCHAR(100),
    city VARCHAR(50)
);

4. Insert sample data into the customers table:

</>
Copy
INSERT INTO customers (username, email, city)
VALUES ('JohnDoe', 'JOHNDOE@EXAMPLE.COM', 'NEW YORK'),
       ('JaneSmith', 'JANESMITH@EXAMPLE.COM', 'Los Angeles'),
       ('CharlieBrown', 'CHARLIEBROWN@EXAMPLE.COM', 'Chicago');
Convert Strings to Lowercase in MySQL - Setup database for examples

With our sample data ready, let’s look at examples of using LOWER() to convert text to lowercase in MySQL.


Examples: Using LOWER() in MySQL Queries

Below are different ways to use the LOWER() function in SQL queries for converting text to lowercase in MySQL.

customers table

1. Converting Usernames to Lowercase

To display each username in lowercase format:

</>
Copy
SELECT username, LOWER(username) AS lowercase_username
FROM customers;

This query returns each username in lowercase, making the output consistent, which is especially useful for login or display purposes.

Convert Strings to Lowercase in MySQL - Example - Converting Usernames to Lowercase

2. Converting Email Addresses to Lowercase

To ensure all email addresses are displayed in lowercase:

</>
Copy
SELECT email, LOWER(email) AS lowercase_email
FROM customers;

This query displays each email in lowercase, which is ideal for case-insensitive email comparisons or consistent formatting in reports.

Convert Strings to Lowercase in MySQL - Example - Converting Email Addresses to Lowercase

3. Using LOWER() for Case-Insensitive Searches

To search for customers in “new york” regardless of the case stored in the city field:

</>
Copy
SELECT *
FROM customers
WHERE LOWER(city) = 'new york';

This query matches all entries with “new york” in any case (e.g., “NEW YORK”, “New York”) by converting both sides of the comparison to lowercase.

Convert Strings to Lowercase in MySQL - Example - Using LOWER() for Case-Insensitive Searches

4. Using LOWER() with CONCAT for Custom Output

To display each username in lowercase with a prefix for custom formatting:

</>
Copy
SELECT CONCAT('User: ', LOWER(username)) AS formatted_username
FROM customers;

This query returns each username in lowercase with the prefix “User: “, like “User: johndoe”, which is useful for personalized reports or displays.

Convert Strings to Lowercase in MySQL - Example - Using LOWER() with CONCAT for Custom Output