How to Convert the Case of a String in PostgreSQL

In PostgreSQL, converting the case of a string is a useful technique that is used to format the strings into a specific case. For this purpose, Postgres provides various built-in functions. These built-in functions can easily transform the case of your strings according to your requirements. The process of letter case functions is simple and straightforward.

This blog demonstrates how to convert the string’s letter case in Postgres via practical examples. So, let’s start!

How Do I Convert the Case of a String in Postgres?

To convert the case of a string in Postgres, you must use one of the following functions:

UPPER(): This function converts all string characters to uppercase.

SELECT UPPER(col_name) FROM table_name;

LOWER(): It converts all characters/letters in a string to lowercase.

SELECT LOWER(col_name) FROM table_name;

INITCAP(): this function transforms each word's first character/alphabet in a string to uppercase and the rest of the characters to lowercase.:

SELECT INITCAP(col_name) FROM table_name;

Example 1: Converting String letters to Uppercase

In the following code, a string is passed to the UPPER(); let's see how it works:

SELECT UPPER('Hello, welcome, how are you');
img

All letters have been converted to UPPER CASE successfully.

Example 2: Converting String letters to Lowercase

In the following code, a string is passed to the UPPER(); let's see how it works:

SELECT LOWER('Hello, WELCOME, how are you');
img

All letters have been converted to lowercase successfully.

Example 3: Converting String letters to Title Case

Let’s learn how the INITCAP() function works in PostgreSQL

SELECT INITCAP('Hello, welcome, how are you');
img

The output proves the working of the INITCAP() function.

Example 4: How to Use Letter case Functions on Table’s Data

We have already created a table named articles_info, whose data is shown in the following snippet:

img

Let’s use the letter case functions in the article_title column:

SELECT LOWER(article_title), UPPER(article_title),
INITCAP(article_title)
FROM articles_info;
img

The output certifies the working of the letter case functions.

Conclusion

In PostgreSQL, the UPPER(), LOWER(), and INITCAP() functions are used for letter case conversion. The UPPER() function converts all the string characters/letters to uppercase, while the LOWER() converts all characters in a string to lowercase. The INITCAP() transforms each word's first character/alphabet in a string to uppercase and the remaining letters/characters to lowercase. This blog presented detailed knowledge on how to convert the case of a string in PostgreSQL.