Código SQL:
Ver originalCREATE TABLE #temp(
fecha datetime
)
INSERT INTO #temp VALUES ('2011-08-08 08:30:00.000')
INSERT INTO #temp VALUES ('2011-08-08 09:30:00.000')
INSERT INTO #temp VALUES ('2011-08-08 10:15:00.000')
INSERT INTO #temp VALUES ('2011-08-08 12:30:00.000')
INSERT INTO #temp VALUES ('2011-08-09 08:45:00.000')
INSERT INTO #temp VALUES ('2011-08-09 09:25:00.000')
INSERT INTO #temp VALUES ('2011-08-09 20:45:00.000')
INSERT INTO #temp VALUES ('2011-08-10 08:32:00.000')
INSERT INTO #temp VALUES ('2011-08-10 08:49:00.000')
INSERT INTO #temp VALUES ('2011-08-10 16:45:00.000')
CREATE TABLE #temp2
(
fecha datetime
)
DECLARE @dias AS INT
DECLARE @fecha1 datetime
DECLARE @fecha2 datetime
DECLARE @fecha3 datetime
DECLARE @x INT
DECLARE @hora1 VARCHAR(20)
DECLARE @hora2 VARCHAR(20)
--'2011-08-08','2011-08-10','08:30:00','09:00:00'
SET @fecha1='2011-08-08'
SET @fecha2='2011-08-10'
SET @hora1='08:30:00'
SET @hora2='09:00:00'
SELECT @dias=datediff(dd,@fecha1,@fecha2)
print @dias
SET @x=0
while @x<=@dias
BEGIN
SET @fecha3=dateadd(dd,@x,@fecha1)
print 'x ' + CONVERT(VARCHAR(2),@x)
print @fecha3
INSERT INTO #temp2
SELECT CONVERT(datetime,fecha) AS fecha FROM #temp WHERE fecha
BETWEEN CONVERT(datetime,(@fecha3 + ' ' + @hora1)) AND CONVERT(datetime,(@fecha3 + ' ' + @hora2))
SET @x=@x+1
END
SELECT * FROM #temp2
Saludos!!!