Click here to Skip to main content
15,903,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have one table name like employee as follow structure
SQL
FirstName      MiddleName        LastName

Raj               null              null

null            Kumar              null

null            null              Reddy


I want output as follows
SQL
Result
------
Raj
Kumar
Reddy

Can you guys please give suggestions how to achieve this output in Query?

Thanks & Regards
Syed Chand Basha
Posted
Updated 26-Jan-15 20:42pm
v2

Try this:
select isnull(firstname, '') + isnull(middlename, '') + isnull(lastname, '') as result from table1

Read: ISNULL (Transact-SQL)[^]
 
Share this answer
 
v2
Comments
CHill60 27-Jan-15 2:58am    
5 from me - you beat me by seconds! I would also have included LTRIM(RTRIM() around it...
@chand5055 - This solution caters for when you have a combination of firstname, middlename, lastname on your table, which is a far more likely situation than your current test data
chand5055 27-Jan-15 4:29am    
Thank you so much.... It is perfectly working.
King Fisher 27-Jan-15 4:59am    
nice 5+ Master ;)
You can use COALESCE function.
SQL
Select COALESCE(FirstName, MiddleName, LastName) as Result
 
Share this answer
 
Comments
CHill60 27-Jan-15 3:21am    
5'd - The most elegant way to solve the problem as stated ...but also see my comment to solution 2 regarding the sample data used
nagendrathecoder 27-Jan-15 4:10am    
Yes you are right.
Try this:
SQL
create table #Test1(firstname nvarchar(max) ,middlename nvarchar(max),lastname nvarchar(max) )

insert into #Test1 values('Raj','','')
insert into #Test1 values('','Kumar','')
insert into #Test1 values('','','Reddy')

option 1:
with cte as(
select firstname From #Test union all select middlename From #Test  union all select lastname From #Test
)
select *From cte where firstname!=''

option 2:
select firstname From #Test where firstname!='' union all select middlename From #Test where middlename!=''  union all select lastname From #Test  where lastname!=''
 
Share this answer
 
v2
Comments
chand5055 27-Jan-15 4:30am    
Hi King Fisher, option2 is working fine. Thank you so much...
King Fisher 27-Jan-15 5:00am    
yeah its good ;)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900