| Author |
Topic |
|
sqlspider
Starting Member
16 Posts |
Posted - 2005-08-29 : 13:27:02
|
| HiWhat is the best way to add two columns into a single column. Lets say I have two columns with first and last names. How can I have a new column with first,last name??Thanks |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2005-08-29 : 13:30:35
|
| SELECT FirstName + ',' + LastNameFROM YourTableTara |
 |
|
|
sqlspider
Starting Member
16 Posts |
Posted - 2005-08-29 : 13:41:47
|
| Wow that was easy. Thanks.How can I create a new column in the same table with the combined first and last names. I triedINSERT INTO tblnew( newcol) values ( SELECT FirstName + ',' + LastNameFROM tblnew)Thanks |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2005-08-29 : 13:47:35
|
| You first need to add the column with ALTER TABLE ADD NewColumnName or you could use Enterprise Manager.Then you'd issue an UPDATE statement.UPDATE YourTableSET NewColumnName = FirstName + ',' + LastNameTara |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2005-08-30 : 00:44:16
|
| >>How can I create a new column in the same table with the combined first and last names. I triedI think there is no need of creating a new columnIn your select query you can show it as seperate columnSelect FirstName,LastName, FirstName+','LastName as FullName from YourTableMadhivananFailing to plan is Planning to fail |
 |
|
|
elwoos
Master Smack Fu Yak Hacker
2052 Posts |
Posted - 2005-08-30 : 03:09:24
|
| You could also set up a View using the same syntax as Tara and Madhivanan have givensteveAlright Brain, you don't like me, and I don't like you. But lets just do this, and I can get back to killing you with beer. |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2005-08-30 : 03:20:54
|
| Or add a computed column to the table (it will work OK, but I hate the things because they have side effects which can become a pain later on - like not being able to do INSERT INTO MyTable without named columns [yeah, bad practice byt handy at times], and difficulties trying to insert through views)Either way probably best to avoid storing the data twice, in different formats - what happens when the combined names get edited and the first/lastname fields don't? How will you know which is more up to date?Kristen |
 |
|
|
Xerxes
Aged Yak Warrior
666 Posts |
Posted - 2005-08-30 : 11:57:51
|
Wow! Tara, Madhi, Elwoos & Kristen all weighing in on the same issue and every one of you had a terrific answer! I learn so much from you folks that I oughta be paying you tuition!! Thanks!!Semper fi, XERXES, USMC(Ret.)------------------------------------------------------The Marine Corps taught me everything but SQL! |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2005-08-31 : 00:59:05
|
Well. Usually there is more than one solution for almost every question MadhivananFailing to plan is Planning to fail |
 |
|
|
|