PostgreSQL INITCAP() function
INITCAP() function
The PostgreSQL initcap() function is used to convert the first letter of each word to upper case and the remaining to lower case.
This function is useful for formatting text data, such as names and titles, to ensure consistent capitalization.
Uses of INITCAP() Function
- Capitalizing Names: Convert names to proper case format (e.g., "axel rush" to "Axel Rush").
- Formatting Titles: Ensure titles and headings have proper capitalization.
- Standardizing Text Data: Convert various text inputs to a consistent case format.
- Combining with Other Functions: Use in conjunction with functions like LOWER() and CONCAT() for complex text transformations.
- Data Presentation: Improve readability of text data in reports and user interfaces by applying proper case.
Syntax:
initcap(<string>)
PostgreSQL Version: 9.3
Pictorial Presentation of PostgreSQL INITCAP() function
Example: PostgreSQL INITCAP() function:
In the example below the initcap function returns to convert the first character or each word in capital and rest are in small letter.
Code:
SELECT initcap('RABINDRANATH TAGORE')
AS "First Character of each word Capital";
Sample Output:
First Character of each word Capital -------------------------------------- Rabindranath Tagore (1 row)
Sample Table: employees
Example of PostgreSQL INITCAP() function using column with nested functions:
If we want to display the first_name, last_name and name in lower case and name in proper case i.e. first letter of each word in capital for those employees who belongs to the department which ID 100, from the employees table, the following sql statement can be used:
Code:
SELECT first_name, last_name,
concat(lower(first_name),' ', lower(last_name)) "Name of Employee in lower case",
initcap(concat(first_name,' ', last_name)) "Proper Name"
FROM employees
WHERE department_id=100;
Sample Output:
first_name | last_name | Name of Employee in lower case | Proper Name -------------+-----------+--------------------------------+------------------- Nancy | Greenberg | nancy greenberg | Nancy Greenberg Daniel | Faviet | daniel faviet | Daniel Faviet John | Chen | john chen | John Chen Ismael | Sciarra | ismael sciarra | Ismael Sciarra Jose Manuel | Urman | jose manuel urman | Jose Manuel Urman Luis | Popp | luis popp | Luis Popp (6 rows)
Previous: CONCAT function
Next: LEFT function
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/PostgreSQL/initcap-function.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics