Solved

Help with SQL Query (Join)

Posted on 2013-06-30
3
590 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 500 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
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…
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.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

734 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