Joining columns using Select in PostgreSQL
MySQL
PostgreSQL is an object-relational database system, which means it can support more complex data types
than its competitors .
Today we will learn how to use SELECT
the operator to join the columns of a table.
Using operators to ||
join columns of type
in PostgreSQLVARCHAR
Now, suppose we create a table with the following configuration.
create table tab(
id int not null,
u_name varchar(50) not null,
PRIMARY KEY (u_name)
);
Then we go ahead and insert some values into it.
insert into tab values(45, 'Jonathon'), (56, 'Mark'), (68, 'Angel');
Finally, SELECT
the concatenation operator in is called for display.
select id || u_name from tab
So we will get the following output.
Output:
How does the above query work? We use ||
Select
the operator in the operation.
||
The operator is defined in the PostgreSQL documentation only as a concatenation string.
Therefore, it is not difficult to join columns where one of the columns must be VARCHAR
of data type, because VARCHAR
corresponds to String
.
Manipulate SELECT
operations to use ||
when columns in PostgreSQL are not strings
SELECT
A very simple modification of the query
given above can be used to join together columns of any other data type.
Now let us assume the following implementation.
create table tab(
id int not null,
age int not null,
PRIMARY KEY (id)
);
insert into tab values(45, 21), (56, 22), (68, 23)
Now we know that both columns are int
; therefore, if you try to do this.
select id || age from tab
It will return an error.
Output:
ERROR: operator does not exist: integer || integer
HINT: No operator matches the given name and argument types. You might need to add explicit type casts.
So, we have two options here:
-
Connect a null between two columns
STRING
and make it a valid operation. - Add a type conversion to one of the column values.
To provide the former option, all you have to do is change your query to something like this.
select id ||''|| age from tab
This returns two numbers concatenating all adjacent columns. It int
concatenates an empty string between the two columns; therefore, the operation is valid for this operator because there is at least one String
.
Connecting in PostgreSQL using Typecast
( ::
notation)
To do this we can use:
select id::text || age from tab
or
select id|| age::text from tab
This type converts one of the columns to Text
type, making the operation valid again because there is now a String
.
In prehistoric PostgreSQL, this notation was commonly used to convert valid types to any other type.
CAST(expression as type)
Using expressions
in PostgreSQL
::
CAST()
The symbol ends with a better counterpart . ::
It is efficient and easier to understand for beginners who just look at the code and don't know what it does.
select CAST(id as varchar(50)) || age from tab
CONCAT(col1, col2)
Using simple operations
in PostgreSQL
If you don't want to do any conversion, make your code as simple as possible. You can switch to concatenation and CONCAT()
functions that accept any given data type.
select concat(id, age) from tab
If a column in PostgreSQL NULL
isCOALESCE(variable, to_type)
If the variable is null, 0, or some other value, we use COALESCE(variable, to_type)
so that it remains valid and is not displayed NULL
.
In our case, NULL
convert our column values to a valid String
type so they can be connected correctly.
Let's use the following configuration.
create table tab(
id int not null,
age int,
PRIMARY KEY (id)
);
insert into tab values(45, 21), (56, NULL), (68, 23);
We inserted one for the second set of values in the table NULL
.
If we do this now.
select id::text || age from tab;
It will return:
Output:
So we can call here COALESCE()
.
select coalesce(id, 0)::text || coalesce(age, 0) from tab;
This tends to return the correct output, with NULL
the values now replaced by 0.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Terminate the PostgreSQL connection
Publish Date:2025/04/11 Views:199 Category:PostgreSQL
-
In this article, we will learn how to terminate a PostgreSQL session. Any open connections are run by background processes or tasks, PSQL which may no longer exist despite exiting the user interface or command line tool. Use ps -ef or grep
Single query to rename and change column type in PostgreSQL
Publish Date:2025/04/11 Views:166 Category:PostgreSQL
-
This article describes how to rename a column and change its type in PostgreSQL using only a single query. Renaming and changing column types in MySQL In MySQL , if you want to change the column type and rename it, you can use a simple stat
Using CASE in PostgreSQL
Publish Date:2025/04/11 Views:124 Category:PostgreSQL
-
This article shows how to use the statement in PostgreSQL CASE . CASE How to use the statement in PostgreSQL case Statements are similar to those in general-purpose programming languages if-else . But in SQL, if you want to write IF-ELSE ,
Using NOT IN with subqueries in PostgreSQL
Publish Date:2025/04/11 Views:93 Category:PostgreSQL
-
NOT IN The inverts the result of NOT simply using IN the operator. NOT IN The right side of the operator must have a subquery in which multiple columns are returned to check whether the expression matches the data. NOT IN Tends to return tr
Using variables in PostgreSQL
Publish Date:2025/04/11 Views:171 Category:PostgreSQL
-
This article will demonstrate how we can declare and assign values to variables in PostgreSQL. In PostgreSQL, DECLARE variables are declared using Often you will need variables in your PL/SQL scripts. In DECLARE the section called , y
Connect to PostgreSQL using a password
Publish Date:2025/04/11 Views:171 Category:PostgreSQL
-
This article shows various ways to connect to PostgreSQL using a password. It can be through the command line, pgpass a file, PGPASSWORD an environment variable or a connection string. Connecting to PostgreSQL with a password using the comm
Deleting a database in PostgreSQL via PSQL
Publish Date:2025/04/11 Views:166 Category:PostgreSQL
-
There are two ways to access PostgreSQL objects and databases on your system. One is through an interface, such as a graphical interface like PGADMIN, and the other is the basic command line tool psql. Today, we will look at DROP DATABASE t
Using the database in PostgreSQL
Publish Date:2025/04/11 Views:132 Category:PostgreSQL
-
This article demonstrates connecting to a database, creating a new database, and creating a table in PostgreSQL. Available databases in PostgreSQL You can run the following command after opening the Postgres command line to view all availab
PostgreSQL replace string
Publish Date:2025/04/11 Views:91 Category:PostgreSQL
-
This article discusses how to use PostgreSQL replace() functions to replace strings. Use the PostgreSQL replace() function to replace strings PostgreSQL replace() functions have the following arguments, which are all text types: replace (st