Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
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.
I have a following table with this columnslno int, effectiveDate dateTimei 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-20102 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 datetimeSET @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
Nageswar9
Aged Yak Warrior
600 Posts
Posted - 2009-03-18 : 00:44:58
Hi see this once
declare @startmonth datetime,@endmonth datetimeselect @startmonth = '1/1/2009' ,@endMonth = '12/31/2009' SELECT number+1 as 's.no',DATEADD(DAY, number, @startmonth) AS 'Date'FROM Master..spt_valuesWHERE type = 'P' AND DATEADD(DAY, number, @startmonth) <= @endMonth
bklr
Master Smack Fu Yak Hacker
1693 Posts
Posted - 2009-03-18 : 00:45:25
try this once
declare @str datetimeselect @str = '1-Jan-2010'select number+1,@str + numberfrom master..spt_valueswhere type ='p'and number < 365