Friday, February 24, 2012

Get Period of Dates

Hi,
Just wonder if i can get a period of dates to be inserted into a temp table (with a single field [Sales_Date]) base on a Start and End Date using a select query?
For Eg,
Start = '8/1/2005', End = '8/5/2005'
In the temp table,
8/1/2005
8/2/2005
8/3/2005
8/4/2005
8/5/2005
Your help is appreciated. Thks.
Rgds
Ryanyou could do something like :

declare @.sdate datetime, @.edate datetime
select
@.sdate = '08/11/2005'
,@.edate = '08/15/2005'
create table #t (datecol datetime)
insert into #t values (@.sdate)
while datediff(d,@.sdate, @.edate) > 0
begin
insert into #t values (dateadd(d, 1,@.sdate ))
set @.sdate = dateadd(d,1, @.sdate)
end
select * from #t
drop table #t|||

Hi,

Your method works. Anyway, aApart from using a temp table, any other more efficient way to get the same outcome? Let me know. Thks.

Ryan

|||The only more efficient way I can think of is to physcially create atable that contains all of the dates and leave that sitting on disk.

No comments:

Post a Comment