Appending a string to an existing field in MySQL
In this article, we will learn to use CONCAT()
the and CONCAT_WS()
functions to concatenate or append string values in MySQL fields.
Use CONCAT() and CONCAT_WS() to append a string to an existing field in MySQL
To understand CONCAT()
and CONCAT_WS()
, let's use the following query to create a category table with two fields: category_id and category_code.
Sample code:
CREATE TABLE category(
category_id INT,
category_code VARCHAR(50)
);
INSERT INTO category (category_id, category_code)
VALUES
(1, 'Windows_1'),
(2, 'Windows_2'),
(3, 'Windows_1');
SELECT * FROM category;
Output:
+-------------+---------------+
| category_id | category_code |
+-------------+---------------+
| 1 | Windows_1 |
| 2 | Windows_2 |
| 3 | Windows_1 |
+-------------+---------------+
3 rows in set (0.00 sec)
Using CONCAT() to append a string to an existing field in MySQL
Now, we use CONCAT()
the method to concatenate the word standard with each value in the category_code column.
Sample code:
UPDATE category SET category_code = CONCAT(category_code, 'standard');
Output:
+-------------+-------------------+
| category_id | category_code |
+-------------+-------------------+
| 1 | Windows_1standard |
| 2 | Windows_2standard |
| 3 | Windows_1standard |
+-------------+-------------------+
3 rows in set (0.02 sec)
CONCAT()
Method takes one or more string type arguments and concatenates them into a single string. This function requires at least one argument; otherwise, an error is raised.
If the expression is a numeric value or a non-binary string, CONCAT()
the method returns a binary string or a non-binary string. Similarly, if the expression is NULL or a binary string, CONCAT()
the method returns NULL or a binary string.
The above output shows that the word standard is appended without any delimiter. To add the delimiter, we execute the query in the following way.
Sample code:
UPDATE category SET category_code = CONCAT(category_code, '_','standard');
Output:
+-------------+--------------------+
| category_id | category_code |
+-------------+--------------------+
| 1 | Windows_1_standard |
| 2 | Windows_2_standard |
| 3 | Windows_1_standard |
+-------------+--------------------+
3 rows in set (0.00 sec)
Suppose we have a string consisting of 3 words and we want to append a separator after it, then we will use the CONCAT() method to do the following.
Sample code:
UPDATE category SET
category_code = CONCAT(category_code,'_','standard1','_','standard2','_', 'standard3');
Output:
+-------------+-----------------------------------------+
| category_id | category_code |
+-------------+-----------------------------------------+
| 1 | Windows_1_standard1_standard2_standard3 |
| 2 | Windows_2_standard1_standard2_standard3 |
| 3 | Windows_1_standard1_standard2_standard3 |
+-------------+-----------------------------------------+
3 rows in set (0.06 sec)
Now, think of 6 words in the string you want to append; the code won't be clean anymore. So, this is CONCAT_WS()
where we use the function.
Using CONCAT_WS() to append a string to an existing field in MySQL
Sample code:
UPDATE category SET
category_code = CONCAT_WS('_',category_code,'standard1','standard2','standard3');
Output:
+-------------+-----------------------------------------+
| category_id | category_code |
+-------------+-----------------------------------------+
| 1 | Windows_1_standard1_standard2_standard3 |
| 2 | Windows_2_standard1_standard2_standard3 |
| 3 | Windows_1_standard1_standard2_standard3 |
+-------------+-----------------------------------------+
3 rows in set (0.00 sec)
Like CONCAT()
, CONCAT_WS()
the method also takes string type parameters. The first parameter is the separator and the rest are the string values we want to append.
This method ignores expressions with NULL values and returns a NULL value if the delimiter is NULL.
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
Display tables and database structure in MySQL
Publish Date:2025/04/23 Views:97 Category:MySQL
-
Today, we will learn about queries in MySQL that can display the table and database structure. We will use the mysqldump utility, DESCRIBE the , SHOW TABLES and SHOW CREATE TABLE the statements. We are using MySQL version 8.0.28 while writi
Select first row from MySQL table
Publish Date:2025/04/23 Views:112 Category:MySQL
-
Today, we will explore three scenarios and their solutions where we want to select the first row from a MySQL table. In the first scenario, we will learn to get the first row from a MySQL table where there are multiple instances of a partic
Insert timestamp into MySQL table
Publish Date:2025/04/23 Views:77 Category:MySQL
-
Today, we will learn how to TIMESTAMP insert date and time into a type column of a MySQL table according to the table definition. Create a MySQL table First, we will create the tables that we will use in this tutorial. Sample code: CREATE T
The difference between two tables in MySQL
Publish Date:2025/04/23 Views:102 Category:MySQL
-
In this article, we will learn how to find the difference between two tables in MySQL. The difference between two tables in MySQL We often need to compare two tables to find records in one table that have no matching records in the other ta
MySQL sorts data alphabetically
Publish Date:2025/04/23 Views:129 Category:MySQL
-
In this article, we will learn about various ways to sort data alphabetically in MySQL. Sort MySQL data alphabetically When you use the SELECT command to query data from a table, the rows in the result set are in arbitrary order. To order t
Display the current database in MySQL
Publish Date:2025/04/23 Views:199 Category:MySQL
-
This article focuses on the various queries that can be used to display the current database in MySQL. We will learn by using the Windows command line and MySQL Workbench. Display the current database in MySQL We can use the following query
Check if a database exists in MySQL
Publish Date:2025/04/23 Views:179 Category:MySQL
-
In this article, we will introduce many ways to check if a database exists in MySQL. Check if the database exists in MySQL The system schema is the schema that MySQL uses. It includes tables that contain data needed by a running MySQL serve
Get the sum of multiple columns in MySQL
Publish Date:2025/04/23 Views:125 Category:MySQL
-
In this article, we will learn how to sum multiple columns in MySQL. Sum multiple columns in MySQL You can use aggregate functions SUM() to calculate the total value in a collection. SUM() The function calculation does not consider NULL val
MySQL ForEach Loop
Publish Date:2025/04/23 Views:164 Category:MySQL
-
This article describes how to simulate a foreach loop in MySQL using INSERT, SELECT, WHERE, and JOIN in one statement. MySQL foreach loop To understand foreach loop simulation, let us create three tables with the following names and attribu