Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Combining columns

Author  Topic 

sqlspider
Starting Member

16 Posts

Posted - 2005-08-29 : 13:27:02
Hi

What 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 + ',' + LastName
FROM YourTable

Tara
Go to Top of Page

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 tried

INSERT INTO tblnew( newcol) values ( SELECT FirstName + ',' + LastName
FROM tblnew)


Thanks
Go to Top of Page

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 YourTable
SET NewColumnName = FirstName + ',' + LastName

Tara
Go to Top of Page

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 tried

I think there is no need of creating a new column
In your select query you can show it as seperate column

Select FirstName,LastName, FirstName+','LastName as FullName from YourTable

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

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 given



steve

Alright 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.
Go to Top of Page

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
Go to Top of Page

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!
Go to Top of Page

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

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -