Search code examples
sqlschedulermultiple-records

SQL Multiple record : Time Scheduler


I have problem about combining tables in store procedure.
Note : field "Time" is varchar
First table (tbTime)

Time
08:00:00
08:30:00
09:00:00
09:30:00
10:00:00
10:30:00
11:00:00
11:30:00
12:00:00
12:30:00
13:00:00
13:30:00
14:00:00
14:30:00
15:00:00
15:30:00
16:00:00
16:30:00
17:00:00
17:30:00
18:00:00
18:30:00
19:00:00
19:30:00
20:00:00

Second table (tbClassRsv)

select * from tbclassrsv where transdate='2014-02-05 00:00:00' and status<>'DEL'

transDate    time       until    status  studentCode  tutor   class    description  userID
2014-02-05   16:48:14   17:48:14 OPN     ET-7201      ET-444  ROOM 01  try          ADMIN

I want the result with condition schedule like this

Time       Student
08:00:00   -
08:30:00   -
09:00:00   -
09:30:00   -
10:00:00   -
10:30:00   -
11:00:00   -
11:30:00   -
12:00:00   -
12:30:00   -
13:00:00   -
13:30:00   -
14:00:00   -
14:30:00   -
15:00:00   -
15:30:00   -
16:00:00   -
16:30:00   ET-7201
17:00:00   ET-7201
17:30:00   ET-7201
18:00:00   ET-7201
18:30:00   -
19:00:00   -
19:30:00   -
20:00:00   -

Thanks for reading or answer ^_^

GBU

I`ve tried this

select t.time, 
isnull(
(select c.studentCode 
from tbclassrsv c 
where c.transdate='2014-02-05 00:00:00' 
and c.class='ROOM 01'
and c.status<>'DEL' 
and t.time>=c.time 
and t.time<=c.until
),'-') [Student] 

The result is....

Time       Student
08:00:00   -
08:30:00   -
09:00:00   -
09:30:00   -
10:00:00   -
10:30:00   -
11:00:00   -
11:30:00   -
12:00:00   -
12:30:00   -
13:00:00   -
13:30:00   -
14:00:00   -
14:30:00   -
15:00:00   -
15:30:00   -
16:00:00   -
16:30:00   -
17:00:00   ET-7201
17:30:00   ET-7201
18:00:00   -
18:30:00   -
19:00:00   -
19:30:00   -
20:00:00   -

Solution

  • Try this. you were not converting your varchar times to datetime so that your time comparisons would work.

    select t.time, 
    isnull(
    (select c.studentCode 
    from tbClassRsv c 
    where c.transdate='2014-02-05 00:00:00' 
    and c.class='ROOM 01'
    and c.status<>'DEL' 
    and DateAdd(MINUTE, 30, Convert(datetime, t.time))>= Convert(datetime, c.time) 
    and Convert(datetime, t.time) <= Convert(datetime, c.until)
    ),'-') from [tbTime] t