Sqlserver
 sql >> Base de Dados >  >> RDS >> Sqlserver

Obter todas as datas no intervalo de datas no SQL Server


Com uma pequena ajuda de uma tabela de números.
declare @T table
(
  ID int identity primary key,
  FromDate date,
  ToDate date
)

insert into @T values
('2011-11-10', '2011-11-12'),
('2011-12-12', '2011-12-14')

select row_number() over(order by D.Dates) as SN,
       D.Dates
from @T as T
  inner join master..spt_values as N
    on N.number between 0 and datediff(day, T.FromDate, T.ToDate)
    cross apply (select dateadd(day, N.number, T.FromDate)) as D(Dates)
where N.type ='P'

Experimente SE Data