SQL Server String UPPER() Function



SQL Server UPPER() Function

The SQL Server UPPER() function is used to convert all characters in a string to uppercase. This function is useful for standardizing text data to a consistent format, especially when performing case-insensitive comparisons.


Syntax

SELECT UPPER(string);

The UPPER() function takes a single argument:

  • string: The string to be converted to uppercase.

Example SQL Server UPPER() Function Queries

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

1. Basic UPPER() Example

SELECT UPPER('Hello World') AS result;

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

result
-------------
HELLO WORLD

2. UPPER() with a Column

SELECT first_name, UPPER(first_name) AS upper_first_name
FROM employees;

This query converts the first_name column to uppercase for each employee. The result will show the first_name and the uppercase version as upper_first_name.

3. UPPER() with a Variable

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

This query uses a variable to store a string and then converts it to uppercase. 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 UPPER() 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 UPPER() Function

This step involves using the UPPER() function to convert the description column to uppercase.

SELECT id, description, UPPER(description) AS upper_description
FROM example_table;

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

id  description  upper_description
--- ------------ -----------------
1   Apple        APPLE
2   Banana       BANANA
3   Cherry       CHERRY

Conclusion

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