Solved

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

Posted on 2014-04-03
10
302 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
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 …
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

856 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