PostgreSQL – How to Concatenate Strings

You can concatenate strings using the || operator or the CONCAT() function in PostgreSQL database. Here are examples of both methods:

Using the || Operator:

The || operator is used to concatenate two or more strings together.

SELECT 'Hello ' || 'World';

You can also concatenate columns from a table:

SELECT first_name || ' ' || last_name AS full_name FROM employees; 

In the example above, the first_name and last_name columns from the employees table are concatenated with a space in between to create a full_name column.

Using the CONCAT() Function:

The CONCAT() function can be used to concatenate multiple strings.

SELECT CONCAT('Hello ', 'World');

When working with columns from a table, you can use the CONCAT() function like this:

SELECT CONCAT(first_name, ' ', last_name) AS full_name FROM employees; 

The result is the same as in the previous example: it concatenates the first_name and last_name columns with a space in between to create a full_name column.

Remember that when concatenating strings, you can include not only string literals but also the results of expressions and functions that return strings. This allows you to build complex strings as needed in your SQL queries.

Similar Posts