convert string case in postgresql

How to Convert String Case in PostgreSQL

Sometimes you may need to convert uppercase string to lowercase or lowercase string to uppercase. In this article, we will look at how to convert string case in PostgreSQL.


How to Convert String Case in PostgreSQL

It is very easy to convert string case in PostgreSQL. We will use lower() function to convert string to lower case, and upper() function to convert string to upper case in PostgreSQL. We will also look at initcap() function that allows you to capitalize first letter of every word in a string.

Also read : How to Concatenate Strings in PostgreSQL


Convert to Lowercase in PostgreSQL

lower() function allows you to convert string to lowercase in PostgreSQL.

Here’s the syntax of lower()

lower(string)

You can provide string as a literal or a column.

Here’s an example to convert literal string to lowercase in PostgreSQL.

select lower('TEST Lower');
   lower
------------
 test lower

Here is an example to convert string column to lower case.

# select first_name,lower(first_name) from employees;
 first_name | lower
------------+-------
 Jim        | jim
 Tim        | tim
 Jane       | jane

Also read : How to Update Multiple Columns in PostgreSQL


Convert to Uppercase in PostgreSQL

upper() function allows you to convert string to uppercase in PostgreSQL.

Here’s the syntax of upper()

upper(string)

You can provide string as a literal or a column.

Here’s an example to convert literal string to uppercase in PostgreSQL.

select upper('test upper');
   upper
------------
 TEST UPPER

Here is an example to convert string column to upper case.

# select first_name,upper(first_name) from employees;
 first_name | upper
------------+-------
 Jim        | JIM
 Tim        | TIM
 Jane       | JANE

Also read : How to Compare Arrays in PostgreSQL


Convert First Character to Uppercase

If you want to capitalize first letter of string then use initcap() function. initcap() function allows you to convert first character of every word in a string into uppercase making it pleasant to read.

Here is the syntax of initcap()

initcap(string)

Here is an example of initcap() to convert first character of every word in a string to uppercase.

# select initcap('test initcap');
   initcap
--------------
 Test Initcap

Need a reporting tool for PostgreSQL? Ubiq makes it easy to visualize data in minutes, and monitor in real-time dashboards. Try it Today!

mm

About Ubiq

Ubiq is a powerful dashboard & reporting platform. Build dashboards, charts & reports for your business in minutes. Try it for free!