SQL Server String LOWER() Function


SQL Server LOWER() Function

The SQL Server LOWER() function is used to convert all characters in a string to lowercase. This function is useful for string manipulation tasks, such as standardizing the case of text data.


Syntax

SELECT LOWER(string);

The LOWER() function takes a single argument:

  • string: The string to be converted to lowercase.

Example SQL Server LOWER() Function Queries

Let's look at some examples of SQL Server LOWER() function queries:

1. Basic LOWER() Example

SELECT LOWER('Hello World') AS result;

This query converts the string 'Hello World' to lowercase. The result will be:

result
-------------
hello world

2. LOWER() with a Column

SELECT first_name, LOWER(first_name) AS lower_first_name
FROM employees;

This query converts the first_name column to lowercase for each employee. The result will show the first_name and the converted lower_first_name.

3. LOWER() with a Variable

DECLARE @myString VARCHAR(50);
SET @myString = 'SQL Server';
SELECT LOWER(@myString) AS result;

This query uses a variable to store a string and then converts it to lowercase. The result will be:

result
----------
sql server

Full Example

Let's go through a complete example that includes creating a table, inserting data, and using the LOWER() function.

Step 1: Creating a Table

This step involves creating a new table named example_table to store some sample data.

CREATE TABLE example_table (
    id INT PRIMARY KEY,
    description VARCHAR(50)
);

In this example, we create a table named example_table with columns for id and description.

Step 2: Inserting Data into the Table

This step involves inserting some sample data into the example_table.

INSERT INTO example_table (id, description) VALUES (1, 'Apple');
INSERT INTO example_table (id, description) VALUES (2, 'Banana');
INSERT INTO example_table (id, description) VALUES (3, 'Cherry');

Here, we insert data into the example_table.

Step 3: Using the LOWER() Function

This step involves using the LOWER() function to convert the description column to lowercase.

SELECT id, description, LOWER(description) AS lower_description
FROM example_table;

This query retrieves the id, description, and the lowercase version of the description column for each row in the example_table. The result will be:

id  description  lower_description
--- ------------ -----------------
1   Apple        apple
2   Banana       banana
3   Cherry       cherry

Conclusion

The SQL Server LOWER() function is a powerful tool for converting all characters in a string to lowercase. Understanding how to use the LOWER() function and its syntax is essential for effective string manipulation and data processing in SQL Server.