Solved

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

Posted on 2014-04-03
10
286 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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
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 video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

747 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

12 Experts available now in Live!

Get 1:1 Help Now