INITCAP: How to Capitalize the First Letter of Every Word in PostgreSQL

Postgres supports various letter case functions to convert the letters from one case to another, such as LOWER(), UPPER(), and INITCAP(). In PostgreSQL, the INITCAP() is used to capitalize the first character/letter of every word. The function name is derived from two words: “INIT” means “initials”, and “CAP” means “Capital”.

This post presents a comprehensive guide on how to use the INITCAP() function in Postgres via practical examples.

What is the INITCAP() Function, and how it Works in Postgres?

The INITCAP() is a built-in string function that accepts a string as an argument and converts the first letter of every word into uppercase and the remaining letters into lowercase:

INITCAP(input_string);

Specify the string of your choice in place of “input_string”.

Example 1: How Does the INITCAP() Work in Postgres?

Pass a string to be converted as an argument to the INITCAP() function and see how the INITCAP() function works:

SELECT INITCAP('welcome to commandprompt.com');
img

The output snippet signifies that the first letter of each word has been successfully converted into uppercase.

Example 2: How Does the INITCAP() Work on Table’s Data in Postgres?

We have created a sample table named “employee_info”, whose data is shown in the following snippet:

img

Let’s use the “INITCAP()” function on the “e_email” column to capitalize the first letter of each word:

SELECT e_name, INITCAP(e_email)
FROM employee_info;
img

The first letter of each word of the selected column has been capitalized successfully.

Example 3: How to Use the INITCAP() Function With CONCAT() Function in Postgres?

The INITCAP() function can be used with different string functions to achieve different functionalities. For instance, the INITCAP() function can be used with the CONCAT() function to concatenate multiple columns and capitalize the first letter of every word:

SELECT e_name, e_email, 
INITCAP(CONCAT(e_name,' - ', e_email)) As emp_details
FROM employee_info;
img

The output snippet clarifies that the selected columns have been concatenated and the first letter of each word in capital.

This is how you can implement the INITCAP() function on the string data.

Conclusion

The INITCAP() function is used in PostgreSQL to capitalize the first character/letter of every word. The function name is derived from two words: “INIT” means “initials”, and “CAP” means “Capital”. The INITCAP() is a built-in string function that accepts a string as an argument and converts the first letter of every word into uppercase and the remaining letters into lowercase. This post presented a comprehensive guide on capitalizing the first letter of every word using the Postgres INITCAP() function.