Share on Google+Share on Google+

MySQL Append String

We use CONCAT() function to append the values of columns.

MySQL Append String


This example illustrates how to append string of different column.

We use CONCAT() function to append the values of columns.

In this example, we are concatenating:

1. The value of "time" column,
2. String ", section" and
3. The value of "sectionid" column

for each row of the "mca" table.

Query to create table "mca":


    studentid  INT NOT NULL,
    sectionid  INT NOT NULL,
    courseid   INT NOT NULL PRIMARY KEY,
    time       VARCHAR(50)

Query to insert values in table:


INSERT INTO mca (studentid,sectionid,courseid,time) VALUES (1,1,6,'Mon 09:30-11:00');
INSERT INTO mca (studentid,sectionid,courseid,time) VALUES (2,2,5,'Mon 11:30-12:00, Thu 09:00-11:00');
INSERT INTO mca (studentid,sectionid,courseid,time) VALUES (3,3,3,'Mon 14:30-16:00');

Query to concatenate value to column values:


SELECT studentid, CONCAT(time, ', section ', sectionid) AS ClassDetails FROM mca;


| studentid | ClassDetails                                |
| 3         | Mon 14:30-16:00, section 3                  |
| 2         | Mon 11:30-12:00, Thu 09:00-11:00, section 2 |
| 1         | Mon 09:30-11:00, section 1                  |


Posted on: October 6, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Discuss: MySQL Append String  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image