Solved

What's the sql statement to insert firstname column and lastname column into fullname column

Posted on 2014-04-03
10
293 Views
Last Modified: 2014-04-06
How do you create a default value of a column that is the concatenated value of two other columns? For example, I have a column for last_name and a column for first_name, and want to create a column for the full_name.
0
Comment
Question by:dynorich
  • 6
  • 4
10 Comments
 
LVL 34

Expert Comment

by:Dan Craciun
ID: 39975218
AFAIK, you cannot use expressions as default values. Only constants and timestamp.

You can use a trigger BEFORE INSERT and one BEFORE UPDATE to obtain about the same effect.

HTH,
Dan
0
 
LVL 34

Accepted Solution

by:
Dan Craciun earned 500 total points
ID: 39975226
BTW, if you already have the data in your table and just want to add the full_name column data, you can do it like this:

UPDATE table SET full_name = CONCAT(first_name, " ", last_name)

Open in new window

0
 

Author Closing Comment

by:dynorich
ID: 39975260
Perfect, thank you
0
 
LVL 34

Expert Comment

by:Dan Craciun
ID: 39975280
Glad I could help!
0
 

Author Comment

by:dynorich
ID: 39981478
Worked fine when I used it on SQL in database.
However when I added it to php page:
Line174  $query = "UPDATE customers SET ";
Line175       $query .= "Fullname = CONCAT{Firstname, " ", Lastname}";
Line176       $query .= " WHERE cid = {$TempID}";
I get the following error:
Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in /home3/dynorich/public_html/extremeleverage/afu/noc/purchase_member.php on line 175.

Any ideas?
0
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
LVL 34

Expert Comment

by:Dan Craciun
ID: 39981517
Why the {}?
$query = "UPDATE customers SET ";
$query .= "Fullname = CONCAT(Firstname, " ", Lastname)";
$query .= " WHERE cid = $TempID";
0
 

Author Comment

by:dynorich
ID: 39981536
I got the error using the() brackets so I was trying the {} brackets. I've changed it back to:
$query = "UPDATE customers SET ";
       $query .= "Fullname = CONCAT(Firstname, " ", Lastname)";
       $query .= " WHERE cid = $TempID";
but still get same error
Would it have anything to do with the " " between Firstname and Lastname
0
 
LVL 34

Expert Comment

by:Dan Craciun
ID: 39981542
Yup.
$query .= "Fullname = CONCAT(Firstname, ' ', Lastname)";
0
 

Author Comment

by:dynorich
ID: 39981550
That worked. Thank you sir.
0
 
LVL 34

Expert Comment

by:Dan Craciun
ID: 39981552
You're welcome!
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Introduction In this installment of my SQL tidbits, I will be looking at parsing Extensible Markup Language (XML) directly passed as string parameters to MySQL 5.1.5 or higher. These would be instances where LOAD_FILE (http://dev.mysql.com/doc/refm…
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://…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

867 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now