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 2005 Forums
 Transact-SQL (2005)
 sql query to insert dates

Author  Topic 

sivaarc
Starting Member

9 Posts

Posted - 2009-03-17 : 23:35:19
I have a following table with this column

slno int, effectiveDate dateTime


i have to insert all the dates for the year 2010 in the table (from 01- jan-2010 to 31-dec-2010). How can i acive this using sql query ?

slno
1 01-jan-2010
2 02-Jan-2010
---
---
--
365 31-Dec-2010



matty
Posting Yak Master

161 Posts

Posted - 2009-03-18 : 00:43:42
You can use a WHILE loop like..

DECLARE @l_effectiveDate datetime
SET @l_effectiveDate = '01/01/2010'
WHILE (@l_effectiveDate < '01/01/2011')
BEGIN
INSERT tablename(effectiveDate) VALUES( @l_effectiveDate)
SET @l_effectiveDate = DATEADD(dd,1,@l_effectiveDate)
END
Go to Top of Page

Nageswar9
Aged Yak Warrior

600 Posts

Posted - 2009-03-18 : 00:44:58
Hi see this once

declare @startmonth datetime,@endmonth datetime
select @startmonth = '1/1/2009' ,@endMonth = '12/31/2009'


SELECT number+1 as 's.no',DATEADD(DAY, number, @startmonth) AS 'Date'
FROM Master..spt_values
WHERE type = 'P'
AND DATEADD(DAY, number, @startmonth) <= @endMonth

Go to Top of Page

bklr
Master Smack Fu Yak Hacker

1693 Posts

Posted - 2009-03-18 : 00:45:25
try this once

declare @str datetime
select @str = '1-Jan-2010'

select number+1,@str + number
from master..spt_values
where type ='p'
and number < 365
Go to Top of Page
   

- Advertisement -