• Thanks so much, i progressed a lot with your inputs, but now i need to loop this whole thing with a table that has multiple startdate and enddate instead of previous question where it was one variable value.

    create table [vendor_sched]

    ([id] [int] IDENTITY(1,1) NOT NULL,

    [FromTime] [varchar](10) NULL,

    [ToTime] [varchar](10) NULL,

    [vendorid] [int] NOT NULL,

    [schedid] [int] NULL)---in a day multiple schedule

    INSERT INTO vendor_sched VALUES('2013-04-03 14:00:00.000','2013-04-03 16:00:00.000',4,1)

    INSERT INTO vendor_sched VALUES('2013-04-04 17:00:00.000','2013-04-04 19:00:00.000',4,2)

    INSERT INTO vendor_sched VALUES('2013-04-04 15:00:00.000','2013-04-04 16:00:00.000',5,1)

    INSERT INTO vendor_sched VALUES('2013-04-04 17:00:00.000','2013-04-04 19:00:00.000',5,2)

    CREATE TABLE [dbo].[book](

    [bookid] [int] IDENTITY(1,1),

    [starttime] [datetime] NULL,

    [endtime] [datetime] NULL,

    [customerid] [int] NULL,

    [vendorid] [int] NULL,

    [timeslotid] [int] NULL,

    ) ON [PRIMARY]

    GO

    INSERT INTO [dbo].[book] VALUES ('2013-04-03 14:00:00.000','2013-04-03 14:30:00.000',100,4,1)

    INSERT INTO [dbo].[book] VALUES ('2013-04-04 18:00:00.000','2013-04-04 18:30:00.000',101,4,2)

    INSERT INTO [dbo].[book] VALUES ('2013-04-04 15:30:00.000','2013-04-04 15:45:00.000',100,5,1)

    Resultset

    --------------

    [vendor_sched].Fromtime [vendor_sched].totime vendorid schedid [book].starttime[book].endtime customerid

    -------------------------------------------------------------------------------------------------------------------------------

    2013-04-03 14:00:00.000 2013-04-03 16:00:00.000 4 1 2013-04-03 14:00:00.000 2013-04-03 14:30:00.000 100

    2013-04-03 14:00:00.000 2013-04-03 16:00:00.000 4 1 2013-04-03 14:30:00.000 2013-04-03 16:00:00.000 NULL

    2013-04-04 17:00:00.000 2013-04-04 19:00:00.000 4 2 2013-04-04 17:00:00.000 2013-04-04 18:00:00.000 NULL

    2013-04-04 17:00:00.000 2013-04-04 19:00:00.000 4 2 2013-04-04 18:00:00.000 2013-04-04 18:30:00.000 101

    2013-04-04 17:00:00.000 2013-04-04 19:00:00.000 4 2 2013-04-04 18:30:00.000 2013-04-04 19:00:00.000 NULL