concat: Adding string to a field data

Concat to add string to data We can append a string or data to an existing data of a field by using concat function of MySQL. Here we are not updating or replacing an existing data with new one, we are just adding the string at the end(or at beginning ) of the field data.

For example we want to add 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 the 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.

update photo_img set dtl=concat(dtl,'site_data to add') where gal_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 directory name as 'html_tutorial' for all the records. We want to update all the records by adding forward slash to the data inside dir name.
UPDATE  `table_name` SET dir=concat('/',dir)
This will add forward slash to the columns dir for all the records ( at left side ).

To add / at end of the data of all records we can use like this.
UPDATE  `table_name` SET dir=concat(dir,'/')
Try to change some part of the data by using replace command. Try to understand difference between replace and concat.

Concat with null data

If null data is present in our record, then we can convert it to empty string and they use concat to add strings.
To find out null data we will use ifnull()
UPDATE  `table_name` SET dir=concat(ifnull(dir,''),'/')

Combining two columns to display as one using select query

We can use concat function to join two columns and display them as single column along with other data. In our table we have first name and last name stored. Now using concat we will combine data of two columns and display them as only name.
SELECT concat(f_name,' ',l_name) as name,class  FROM `student_name`
This will return the following records.
John DeoFour
Garry MillerFive
Note that we have kept some null data in our records , in the above query if f_name or l_name is null then the total value after concat will be null. If we require that it should ignore the null data and display only the available data then we have to use COALESCE and display
SELECT concat(COALESCE(f_name,'-',' '),' ',COALESCE(l_name,'-')) as name,class  FROM `student_name`
With this we will get output like this.
John Deo Four
Larry - Four
Ronald - Five
Garry Miller Five
- - Five
- Ruller NULL

Joining three columns with separator

SELECT CONCAT ( f_name,'/',l_name,'/',class) as file_name FROM `student_name`
Here is the sql dump of student_name table for your use.
  `f_name` varchar(20) DEFAULT NULL,
  `l_name` varchar(20) DEFAULT NULL,
  `class` varchar(20) DEFAULT NULL

-- Dumping data for table `student_name`

INSERT INTO `student_name` (`f_name`, `l_name`, `class`) VALUES
('John', 'Deo', 'Four'),
('Larry', NULL, 'Four'),
('Ronald', NULL, 'Five'),
('Garry', 'Miller', 'Five'),
(NULL, NULL, 'Five'),
(NULL, 'Ruller', NULL);

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?
SELECT name, mark,(mark*1) AS  mark2 FROM student
For all numeric values in above query we will get both columns same value. When a string is multiplied by 1 we get output as zero. Now we will use this concept to list out which are not matching.
SELECT name, mark FROM student   WHERE CONCAT('',(mark*1)) !=mark
This will give us non numeric value in Mark field.
4Krish StarFourwrong datafemale
7My John RobFifthstring2male
To test this code, change the mark column to Varchar and add some string data to mark field.

Add Bold html tag to data
SELECT concat('<b>',concat_ws(f_name,' ',l_name,'</b>') ) as name,class  FROM `student_name`

CONCAT_WS : concatenate with separator

Visitors Rating
Your Rating



I tried but it didn't work. Which version of MySQL did you try? Mine is 4.2


What is the error message you are getting? Are you getting message saying function concat does not exit ?


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.


This only works if the value of the field is not NULL.... FYI ;)


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

Post your comments , suggestion , error , requirements etc here .

We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
©2000-2019 All rights reserved worldwide Privacy Policy Disclaimer