Click here to Skip to main content
15,920,633 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi sir,

The below one is the out put of my select query
CityName1	CityName2
Bangalore	Delhi
Delhi		Mumbai
Mumbai		Bangalore

I need to display it as
Bangalore - Delhi - Mumbai - Bangalore

Please help me for achieving this.

Thank you
Posted
Updated 23-Sep-14 23:32pm
v2
Comments
Sinisa Hajnal 24-Sep-14 5:19am    
So you need single row with distinct values from two columns?
Eduard Keilholz 24-Sep-14 5:38am    
I don't see the distinct, there's two Bangalore values...
Sinisa Hajnal 24-Sep-14 5:59am    
Indeed, but there are no Delhi and Mumbai twice...this looks like train / bus / plane / whatever travel route
kanamala subin 24-Sep-14 5:25am    
yes exactly sir
George Jonsson 24-Sep-14 5:40am    
What is your current SELECT query?

Try this:

SQL
declare @route nvarchar(max),@route1 nvarchar(max)
select @route = CityName1 from tbl_map where ID=1//here get your cityname1 top1
SELECT @route1 =STUFF((SELECT '-' + cast(CityName2 as varchar) AS [text()] FROM tbl_map  FOR XML PATH('') ), 1, 1, '' )  
select @route+'-'+@route1
 
Share this answer
 
DECLARE @route varchar(max)

SQL
SELECT
    @route = ISNULL(@route, '') + CASE WHEN @route IS NULL THEN '' ELSE ' - ' END + 
        CityName1
FROM
    your_table


Add to that whatever you need at the end...if you describe a bit better and show your query I can make it more precise
 
Share this answer
 
v2
Comments
King Fisher 24-Sep-14 6:23am    
my 5+

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