We can append a string of data to an existing data of a field by using concat function of MySQL. Here we are not updating or replacing existing data with a new one, we are just adding the string at the end(or at the beginning ) of the field data.
SELECTCONCAT('string1', '-', 'string2')
Output
string1-string2
Joining strings by using CONCAT and handling NULL value.
For example, we want to add a site signature at the end of the comments posted by users. Here each user posts are different so we can't apply any update command so we will use concat to add the site signature to each post kept inside a record field in our update command.
Here is the syntax
CONCAT(field_name, "string to add")
Now let us see how it is used in a MySQL table query.
UPDATEphoto_imgSETdtl = CONCAT(dtl, 'site_data to add')
WHEREgal_id = '22'
The above command will add the string site_data to add at the end of each record dtl field for which gal_id is equal to 22
If we will not use Where condition in sql then all the records will be changed with concat query.
Adding / to dir name stored in MySql table
In our table, we stored the directory name as 'html_tutorial' for all the records. We want to update all the records by adding forward slash to the data inside the dir name.
UPDATE`table_name`SETdir = CONCAT('/', dir)
This will add forward slash to the columns dir for all the records (on the left side ).
To add / at end of the data of all records we can use like this.
UPDATE`table_name`SETdir = CONCAT(dir, '/')
Try to change some part of the data by using replace command. Try to understand difference between replace and concat.
Combining two columns to display as one using select query
We can use the Concat function to join two columns and display them as a single column along with other data. In our table we have a first name and last name stored. Now using concat we will combine data of two columns and display them as the only name.
Finding non integer values in an Integer field using concat
We have stored mark in the student table. But there are some string data is stored in the mark column and mark column is set as Varchar field. How to select these non-numeric values?
SELECTname, mark,
(mark * 1) ASmark2FROMstudent
For all numeric values in the above query, we will get both columns the same value. When a string is multiplied by 1 we get the output as zero. Now we will use this concept to list out which are not matching.
SELECTname, markFROMstudentWHERECONCAT('', (mark * 1)) != mark
This will give us non-numeric value in mark field.
id
name
class
mark
sex
4
Krish Star
Four
wrong data
female
7
My John Rob
Fifth
string2
male
To test this code, change the mark column to Varchar and add some string data to mark field.
Passionate about coding and teaching, I publish practical tutorials on PHP, Python,
JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and
project‑oriented with real examples and source code.
This article is written by plus2net.com team.
https://www.plus2net.com
plus2net.com
mroz
06-02-2009
I tried but it didn't work. Which version of MySQL did you try? Mine is 4.2
smo
06-02-2009
What is the error message you are getting?
Are you getting message saying function concat does not exit ?
Ramya
06-11-2009
it did work but not in the way i wanted.It was wacky it updated by id field.I mentioned the column name and string to append and but it updated by id field. i have no idea.
Jim
19-01-2010
This only works if the value of the field is not NULL.... FYI ;)
Neeraj
18-03-2010
CONCAT_WS() is the inbuilt function to come out of this issue.
Example: update test_user set descrip = concat_ws(' ',descrip, 'SECOND') where Id=2
✖
We use cookies to improve your browsing experience. . Learn more