Link to home
Start Free TrialLog in
Avatar of Johny Bravo
Johny Bravo

asked on

Help with SQL Query (Join)

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?
Avatar of ianmills2002
ianmills2002
Flag of Australia image

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

Avatar of Johny Bravo
Johny Bravo

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of ianmills2002
ianmills2002
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial