Solved

Help with SQL Query (Join)

Posted on 2013-06-30
3
563 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 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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…

758 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now