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
 Merge 2 columns into 1

Author  Topic 

david_reinjal
Starting Member

36 Posts

Posted - 2007-12-07 : 01:22:38
hi guys,

i have a table with 2 columns say:

First_Name Last_Name
David Reinjal

i need to merge these 2 columns and put into a single column with a ','(comma) seperated. it will look loke this:

column_name
David, Reinjal

how can i do this? can anybody help me in this?

thanks,
David

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2007-12-07 : 01:46:06
select first_name +','+coalesce(last_name,'') as column_name from table

Madhivanan

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

david_reinjal
Starting Member

36 Posts

Posted - 2007-12-07 : 01:54:25
thnx man..but actually i wanted multiple rows into a single record. like:
First_name
David
Madhivanan

this has to be put in a single record with a ',' seperator. but i need to use a where clause to specify which names in a state should be merged. for instance:

State First_name
Kar David
kar Madhivanan
Mum Mohan

so all the names in Kar should me merged in a single record with a ',' seperator.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2007-12-07 : 02:04:09
If you use front end application, you can concatenate them
or refer http://sqlblog.com/blogs/adam_machanic/archive/2006/07/12/rowset-string-concatenation-which-method-is-best.aspx

Madhivanan

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

Koji Matsumura
Posting Yak Master

141 Posts

Posted - 2007-12-07 : 02:10:28
or maybe
select RTRIM(first_name) +','+coalesce(last_name,'') as column_name from table
Go to Top of Page
   

- Advertisement -