Solved

Concatenate string in MySQL update query

Posted on 2009-07-13
4
722 Views
Last Modified: 2012-05-07
I need to update all the fields in table based on other fields

field1 = field2 + "_" + field3 + "_" + field4

I have have the use of CONCAT but not sure how to use it.

Thanks
0
Comment
Question by:SaltyDawg
  • 2
4 Comments
 
LVL 59

Accepted Solution

by:
Kevin Cross earned 275 total points
ID: 24841446
You can use the CONCAT with separator like this:
update your_table
set field1 = concat_ws('_', field2, field3, field4);

Open in new window

0
 
LVL 45

Assisted Solution

by:Kdo
Kdo earned 225 total points
ID: 24841460
Hi SaltyDawg,

Exactly which concatenation operator you can use depends on the SQL parser settings.  :)

Regardless of the shortcut, you can always call the CONCAT function.  Just list the items to be concatenated.

  CONCAT (field2, "_", field3, " ", field4);


Good Luck,
kent
0
 
LVL 1

Author Comment

by:SaltyDawg
ID: 24841651
Thanks
I actually used both concat_ws and CONCAT and orginally it did not work but I see my errors. I had quotes around my field names.
0
 
LVL 1

Author Closing Comment

by:SaltyDawg
ID: 31602905
I shared your points. Since mwvisa1 responded first and i did use the concat_ws method I award him more points. Thanks to you both.
0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Foreword This is an old article.  Instead of using the MySQL extension that was used in the original code examples, please choose one of the currently supported database extensions instead.  More information is available here: MySQLi / PDO (http://…
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question