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)
 how to insert incremented date starting from previous date

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2005-05-26 : 08:14:39
ashish writes "I need to insert date in a column which should start from 1st jan 2005 and keep on inserting date till today. How should I achieve that.
I tried :- But it is inserting same date. Whats wrong with dateadd function.
===============================================================
Declare @today Datetime
Declare @todaydate datetime
set @today = '2005-01-01'

WHILE (SELECT Today=GETDATE()) > '2005-01-01'
BEGIN

Insert tablename(date_insert)
Values (@todaydate)
select @todaydate = dateadd(day,1,@today)

print @todaydate

IF (SELECT Today=GETDATE()) < '2005-05-25'
BREAK
ELSE
CONTINUE

END"

derrickleggett
Pointy Haired Yak DBA

4184 Posts

Posted - 2005-05-26 : 08:24:02
I think you tried to make this waaaaaaay too complicated.


DECLARE @table TABLE(datecol DATETIME)

DECLARE @today DATETIME
SET @today = '2005-01-01'

WHILE @today <= GETDATE()
BEGIN

INSERT @table(datecol)
VALUES (@today)
SELECT @today = DATEADD(day,1,@today)

PRINT @today

END


SELECT datecol FROM @table


MeanOldDBA
derrickleggett@hotmail.com

When life gives you a lemon, fire the DBA.
Go to Top of Page
   

- Advertisement -