Solved

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

Posted on 2014-04-03
10
308 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
10 Comments
 
LVL 35

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 35

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
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 35

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
 
LVL 35

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 35

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 35

Expert Comment

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

Featured Post

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
mysql query for sum() 3 58
MySQL 5.6.30 - daily outages 46 86
embadded search engine in website 4 46
MySQL 6 46
More Fun with XML and MySQL – Parsing Delimited String with a Single SQL Statement Are you ready for another of my SQL tidbits?  Hopefully so, as in this adventure, I will be covering a topic that comes up a lot which is parsing a comma (or other…
Foreword This article was written many years ago, in the days when PHP supported the MySQL extension (http://php.net/manual/en/function.mysql-connect.php).  Today (http://php.net/manual/en/migration70.removed-exts-sapis.php) you would not use MySQL…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

737 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