PostgreSQL Tutorial
Data Types
Querying & Filtering Data
Managing Tables
Modifying Data
Conditionals
Control Flow
Transactions & Constraints
Working with JOINS & Schemas
Roles & Permissions
Working with Sets
Subquery & CTEs
User-defined Functions
Important In-Built Functions
PostgreSQL PL/pgSQL
Variables & Constants
Stored Procedures
Working with Triggers
Working with Views & Indexes
Errors & Exception Handling
The UPPER
function in PostgreSQL is a built-in string function that is used to convert all characters of a given text string to uppercase. This function can be particularly useful when you're performing case-insensitive searches or comparisons.
UPPER(text_string)
Where text_string
is the string value you want to convert to uppercase.
Basic Usage:
SELECT UPPER('PostgreSQL is amazing!');
Result:
POSTGRESQL IS AMAZING!
Using UPPER in WHERE Clause:
Let's say you have a table named employees
and you want to search for an employee with the name 'john doe', regardless of how the name is cased in the table. You can use the UPPER
function to perform a case-insensitive search:
SELECT * FROM employees WHERE UPPER(employee_name) = UPPER('john doe');
Combining with Other String Functions:
You can combine the UPPER
function with other string functions for more complex operations. For example, to get the first 5 characters of a string in uppercase:
SELECT UPPER(SUBSTRING('PostgreSQL' FROM 1 FOR 5));
Result:
POSTG
The UPPER
function only affects alphabetic characters. Numerals, special characters, and whitespace remain unchanged.
If you need to convert text to lowercase, you can use the complementary LOWER
function.
Using UPPER
(or LOWER
) can be a useful way to normalize data for case-insensitive matching, but be mindful that using these functions in WHERE
clauses can prevent the use of indexes, potentially leading to slower query performance. If case-insensitive operations are frequent, consider other solutions like creating a case-insensitive index using the citext
module or storing data in a normalized form.
In summary, the UPPER
function in PostgreSQL allows you to convert text strings to uppercase, facilitating case-insensitive operations and text normalization.
PostgreSQL UPPER function example:
Use the UPPER
function to convert a string to uppercase:
SELECT UPPER('example');
How to use UPPER function in PostgreSQL:
Apply the UPPER
function to convert a string to uppercase:
SELECT UPPER(column_name) FROM example_table;
Convert to uppercase in PostgreSQL:
Convert a column to uppercase using the UPPER
function:
SELECT UPPER(name) AS uppercase_name FROM employees;
UPPER function with SELECT statement in PostgreSQL:
Use the UPPER
function in the SELECT
statement:
SELECT id, UPPER(name) AS uppercase_name FROM example_table;
Case-insensitive search with UPPER in PostgreSQL:
Perform a case-insensitive search using UPPER
:
SELECT * FROM example_table WHERE UPPER(column_name) = UPPER('search_value');
UPPER vs INITCAP in PostgreSQL:
Compare UPPER
and INITCAP
for uppercase conversions:
SELECT UPPER('example'), INITCAP('example');
Using UPPER with WHERE clause in PostgreSQL:
Apply UPPER
in the WHERE
clause for case-insensitive filtering:
SELECT * FROM example_table WHERE UPPER(column_name) = UPPER('filter_value');
Concatenate UPPER with other string functions in PostgreSQL:
Combine UPPER
with other string functions:
SELECT UPPER(CONCAT('prefix_', column_name, '_suffix')) FROM example_table;
UPPER function and collation in PostgreSQL:
Use collation for case-sensitive or case-insensitive sorting with UPPER
:
SELECT * FROM example_table ORDER BY UPPER(column_name) COLLATE "en_US";