Question

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   -
Was it helpful?

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

OTHER TIPS

What you need to do is round c.time down to the nearest 30 minutes interval, and round c.until up to the nearest interval. This way your where clause will have the correct range.

To do the rounding you will need to convert the times to datetime which you can do like so:

CAST(CONVERT(varchar,THE_TIME_AS_VARCHAR,121) AS datetime)

Then you can round down to the nearest 30 minutes like so:

DATEADD(mi, DATEDIFF(mi, 0, THE_TIME_AS_DATETIME)/30*30, 0)

And round up like so:

DATEADD(mi, DATEDIFF(mi, 30, THE_TIME_AS_DATETIME)/30*30, 0)

Applying all that to your existing code would give you 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>= DATEADD(mi, DATEDIFF(mi, 0, CAST(CONVERT(varchar,c.time,121) AS datetime))/30*30, 0) 
    and t.time<= DATEADD(mi, DATEDIFF(mi, 30, CAST(CONVERT(varchar,c.until,121) AS datetime))/30*30, 0)
),'-') [Student] 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top