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

Author  Topic 

shanmugaraj
Posting Yak Master

219 Posts

Posted - 2006-04-18 : 03:42:59

i have 2 fields name_last , name_first
when i use the query ,
Select name_last , name_first from members order by 1,2

i get the values..
Now I want to merge the fileds and show in a single column like Name_last , Name_first

ie ::

Select name_last , name_first from members order by 1,2 =
Col1 Col2
Aagersen Kevin

Select name_last " , " name_first from members order by 1,2 =

ColumnMerge1
Aagersen , Kevin



jen
Master Smack Fu Yak Hacker

4110 Posts

Posted - 2006-04-18 : 04:09:09
concatenate them using '+' instead of comma



--------------------
keeping it simple...
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2006-04-18 : 04:09:32
Select name_last + " , " + name_first from members order by name_last,name_first


Madhivanan

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

shanmugaraj
Posting Yak Master

219 Posts

Posted - 2006-04-18 : 05:02:49
when i try
Select name_last + " , " + name_first from members order by name_last,name_first

Error
Server: Msg 207, Level 16, State 3, Line 1
Invalid column name ' , '.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2006-04-18 : 05:06:03
Instead of double quotes use single quote

Select name_last + ' , ' + name_first from members order by name_last,name_first



Madhivanan

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

shanmugaraj
Posting Yak Master

219 Posts

Posted - 2006-04-18 : 05:15:26
Thanks .. IT Works

Select name_last + ' , ' + name_first as full_Name from members order by name_last,name_first


how to split the full_Name for saving to individal column where , occurs
I:e i want to split full_Name where , comes and save in two columns
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2006-04-18 : 05:34:13
[code]

select left(full_name,charindex(',',full_name)-1) as first_name,
substring(full_name,charindex(',',full_name)+1,len(full_name)) as second_name
from
(
select 'foo,bar' as full_name union all
select 'test,bar' as full_name union all
select 'more,test' as full_name
) T
[/code]
Also refer this split function
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=50648

Madhivanan

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

shanmugaraj
Posting Yak Master

219 Posts

Posted - 2006-04-18 : 05:43:49
Thanks. It works
Go to Top of Page
   

- Advertisement -