last day (14 days later) » 

12:43 PM
0
A: How to select the time a room is available given date and room in SQL Server?

deanAssuming no overlaps, and minutes as the smallest time unit, and of course the Numbers table, for SQL Server 2012 this should work: declare @date datetime = '20170102', @room int = 14 ;with x as ( select num.n, isnull(lag(num.n) over(order by num.n),-1) n_prev from utility.numbers num ...

 
How do I integrate this to my sp? I don't know what the objects represent in my sp. I understand that @t is Reservation table in my sp. What does utility.numbers represent? Its giving me errors
 
Yes, @t represents the Reservation table, I edited to reflect this. I also included an SE link with more information on Numbers table (how to create and what to use it for).
 
Could you give me the exact script to create and insert the numbers table you speak off. I'd be happy to test it. I honestly don't understand how this works.
 
Did you check the link I added to the answer? If not, pls do.
 
I have tried it. I doesn't show the available schedule prio to the first schedule record. Asside from that it was good. Your answer has the same problems as the other answers here.
 
12:43 PM
It does, actually. It shows free slot before the first schedule, after the last schedule, and also in case there is no schedule for the given day.
 
I tried it again and it doesn't
It also doesnt show whole day when there are no reservations on the room and date
 
I really can't tell why this doesn't work for you, and don't think we should go any deeper into this here. If you wish, contact me on my private email to discuss it further.
 
What is your email. I'll send you all the scripts I executed.
 
On my profile page.
 
I sent you an email
Reservation Table

CREATE TABLE [dbo].[Reservation](
[ReservationID] [int] IDENTITY(1,1) NOT NULL,
[ReservationDateFrom] [datetime] NOT NULL,
[ReservationDateTo] [datetime] NOT NULL,
[ReservationRoom] [int] NOT NULL
PRIMARY KEY CLUSTERED
(
[ReservationID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]


Numbers table(copy pasted)
CREATE TABLE Numbers
(
Number INT NOT NULL,
 
12:56 PM
Oh, I see, the Numbers table starts at 0 for you (as opposed to starting with 1 in my table).
Here's the changed query:
declare @date datetime = '2017-02-17', @room int = 6003
;with x as (
select num.Number, isnull(lag(num.Number) over(order by num.Number),-1) n_prev
from #Numbers num
left join #Reservation t on num.Number between datediff(minute, @date, reservationdatefrom) and datediff(minute, @date, reservationdateto) and t.reservationroom = @room
where num.Number < 1440 and num.Number > 0
and t.reservationid is null
)
select *, cast(dateadd(minute, Number-1, @date) as time), cast(dateadd (minute, isnull(lead(n_prev+1) over(order by Number), 1440), @date) as time),
 
trying
Worked!
 
cool
I'll edit the answer to include n > 0
 

last day (14 days later) »