Reputation: 4732
Table1
HolId
1 Package17 2,1,5
2 Package13 5,4,3
Table 2
HolId1
1 New Years Day 2013-01-01 00:00:00.000
2 Republic Day 2013-01-26 00:00:00.000
3 Holi 2013-04-27 00:00:00.000
4 Memorial Day 2013-05-27 00:00:00.000
5 US Independence 2013-07-04 00:00:00.000
7 Labour Day 2013-09-02 00:00:00.000
I want to display data like below
HolId HolId1
Package17 2 2 Republic Day 2013-01-01 00:00:00.000
Package17 1 1 New Years Day 2013-04-27 00:00:00.000
Package17 5 5 US Independence 2013-05-27 00:00:00.000
Package13 5 5 US Independence 2013-07-04 00:00:00.000
Package13 4 4 Memorial Day 2013-05-27 00:00:00.000
Package13 3 3 Holi 2013-04-27 00:00:00.000
My grid will be displayed as follows which i have figured out
Package17
Republic Day 2013-01-01 00:00:00.000
New Years Day 2013-04-27 00:00:00.000
US Independence 2013-05-27 00:00:00.000
Package13
US Independence 2013-07-04 00:00:00.000
Memorial Day 2013-05-27 00:00:00.000
Holi 2013-04-27 00:00:00.000
Upvotes: 1
Views: 656
Reputation: 2594
Better to de-normalize your table, then just one join you can get your result. If you have to use the current schema. Hope the following code help you.
declare @tempTable table
( Package_Name varchar(100),
Holid int
)
declare @SQLString varchar(max)
declare @package_name varchar(100)
set @SQLString = ''
DECLARE @X XML
while @SQLString is not null
begin
set @SQLString = null
select top 1 @package_name=package_name, @SQLString = Holid
from Package
where package_name not in (select package_name from @tempTable)
if @SQLString is null
break
SET @X = CAST('' + REPLACE(@SQLString, ',', '') + '' AS XML)
insert into @tempTable
SELECT @package_name, t.value('.', 'INT')
FROM @x.nodes('/A') as x(t)
end
select * from @tempTable
select A., B.* from @tempTable A
inner join HolID1 B
on A.Holid = B.HolID1
Upvotes: 0
Reputation: 33381
This is solution for MySQL
:
SELECT
T1.Pkg, T2.*
FROM Table2 T2
JOIN Table1 T1
ON FIND_IN_SET(T2.HolID1, T1.HolID)
and this is for SQL SERVER
SELECT
T1.Pkg, T2.*
FROM Table2 T2
JOIN Table1 T1
ON CHARINDEX(CAST(T2.HolID1 as varchar), T1.HolID) > 0
Upvotes: 4