Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Help with SQL Query (Join)

Posted on 2013-06-30
3
Medium Priority
?
619 Views
Last Modified: 2013-07-01
Hi Experts,

I have two tables
1> Amenity
AmenityId (Int,PK)
Amenity (varchar(200)

2> HotelDetails
HotelId (int,PK)
HotelName (varchar(250)
AmenityId (varchar(200))


As you can see in hotelDetails amenity Ids are stored as CSV.
Like
2,4,7,8

Now I want to display Amenities of Hotels.
How to join these tables?
0
Comment
Question by:johny_bravo1
  • 2
3 Comments
 
LVL 6

Expert Comment

by:ianmills2002
ID: 39289146
Ideally the Hotel Details table should have one row per amenity for an easy table join.

I think you can try something like this. Please excuse my concatantion syntax if I have it wrong for SQL Server

SELECT *
FROM HotelDetail H
          JOIN Amenity A
          ON ',' || H.AmenityID || ',' like '%,' || CAST(A.AmenityId as varchar(10)) || ',%'

Open in new window

0
 
LVL 8

Author Comment

by:johny_bravo1
ID: 39289166
Hi Expert,

Thanks for the reply

>>Ideally the Hotel Details table should have one row per amenity for an easy table join
Agreed but can't change the table definition.

The query given by you is not working in SQL server
0
 
LVL 6

Accepted Solution

by:
ianmills2002 earned 2000 total points
ID: 39289187
Try this one. The || were invalid in SQL Server. I changed them to +

SELECT *
FROM HotelDetail H
          JOIN Amenity A
          ON ',' + H.AmenityID + ',' like '%,' + CAST(A.AmenityId as varchar(10)) + ',%'

Open in new window

0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question