Solved

Help with SQL Query (Join)

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
backup and restore 21 30
SQL Query 2 34
VB.NET Application Installation with sqlserver 8 32
SQL Recursion schedule 13 19
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
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…
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.

820 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