Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Using loop to split data?

Author  Topic 

phoenix22
Starting Member

20 Posts

Posted - 2005-12-12 : 21:23:09
I need to create a table with two columns. Basically, the inputs would be two dates (Start Date and End Date), and the created table would contain two columns with the date range broken out in 2 week intervals. So if my date range was 12/4/2005 to 12/31/2005, the table would look like below:

START_DATE END_DATE
12/4/2005 12/17/2005
12/18/2005 12/31/2005

I know I need a loop of some sort, does anyone have any idea as to how I could do this?

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2005-12-12 : 21:32:20
Simple while loop solution
declare
@date_start datetime,
@date_end datetime

select @date_start = '2005-12-04',
@date_end = '2005-12-31'

while @date_start <= @date_end
begin
select @date_start, dateadd(day, 13, @date_start)
select @date_start = dateadd(day, 14, @date_start)
end


-----------------
[KH]

Go to Top of Page

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

7020 Posts

Posted - 2005-12-12 : 22:15:32
This statment will do it without a loop using a number table.

You can get the code for the F_TABLE_NUMBER_RANGE function at:
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=47685


select
date_start= dateadd(day, (a.number*14), a.dt),
date_end = dateadd(day, (a.number*14)+13, a.dt)
from
(
select
aa.number,
DT = convert(datetime,'2005/12/04')
from
dbo.F_TABLE_NUMBER_RANGE(0,99) aa
) a






CODO ERGO SUM
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2005-12-12 : 22:27:42
The F_TABLE_NUMBER_RANGE is really cool

-----------------
[KH]

Learn something new everyday
Go to Top of Page
   

- Advertisement -