Click here to Skip to main content
12,243,752 members (43,965 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: SQL
hello all

i am creating a project of Hotel management in asp.net in which i have many branches in all cities and name of hotel is same.
now i am creating a table where i am selecting HOTEL_NAME and CITIES
like HOTEL NAME is "pigion" and i have some cities like "QQ" "YY" "ZZ"

i wrote a query as " Select HOTEL_NAME,CITIES from HOTEL ;
it return data like:
HOTEL_NAME          CITIES
Pigion                        QQ  
Pigion                        YY      
Pigion                                ZZ 
now i want the OUTPUT in single ROW like:
HOTEL_NAME                CITIES
Pigion                              QQ,YY,ZZ
for that i can show this into my project in a single line i am using DataList for showing all
the records.

Please help me.
Posted 16-Feb-13 3:00am
Edited 16-Feb-13 3:29am
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

This might be helpful
DECLARE @CITIES VARCHAR(8000)  
SELECT @CITIES = COALESCE(@CITIES + ', ', '') + CITIES FROM Hotels
 
SELECT distinct HOTEL_NAME, @CITIES AS CITIES FROM Hotels
  Permalink  
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

Hi Jackson,


Try This.....

-- Table Creation
IF OBJECT_ID('TempDB..#HotelDtls') IS NOT NULL DROP TABLE #HotelDtls
CREATE TABLE #HotelDtls(HotelName VARCHAR(50), CityName VARCHAR(100))
-- Sample Data
INSERT INTO #HotelDtls(HotelName, CityName) VALUES('Pigion','QQ'),('Pigion','YY'), ('Pigion','ZZ')
-- Actual Data
SELECT HotelName, CityName FROM #HotelDtls
-- Required Output
SELECT HotelName,  
  REPLACE((SELECT  CityName AS [data()] 
           FROM #HotelDtls  
           WHERE b.HotelName=HotelName
           ORDER BY CityName FOR XML PATH('')),
          ' ', ', ') 'CityName'
FROM #HotelDtls b 
GROUP BY HotelName



Regards,
GVPrabu
  Permalink  
Comments
jackson kumar 18-Feb-13 0:13am
   
Thanks
Mr. Ramesh And Prabu thanks a lot.
cheers

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160426.1 | Last Updated 17 Feb 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100