Solved

Populate table with dates.

Posted on 2011-02-14
5
461 Views
Last Modified: 2012-05-11
I have for example a name 'John' and a start date '1990-01-01' and an end date '2011-01-01'

My destination table is [Name], [Date]

How would I insert an entry for each day in the destination using a single query?.


Example

[NAME]      [DATE]
John          1900-01-01
John          1900-01-02
John          1900-01-03
John          1900-01-04
John          1900-01-05
....................................
....................................
....................................
John          2010-12-30
John          2010-12-31
John          2011-01-01


0
Comment
Question by:koossa
5 Comments
 
LVL 7

Accepted Solution

by:
mkobrin earned 500 total points
ID: 34886553
declare @date datetime
set @date = '1900-01-01'
while @date <= '2011-01-01'
BEGIN
insert into tableName(Name, Date) values('John', @date)
select @date = dateadd(dd, 1, @date )
END
0
 
LVL 11

Expert Comment

by:rajvja
ID: 34886574
0
 
LVL 21

Expert Comment

by:Alpesh Patel
ID: 34886578
Hi,

Use code block to insert date from start date to end date. or use cursor. Run till end date and insert data in table.
0
 
LVL 10

Expert Comment

by:John Claes
ID: 34886600
Koossa,

the easy way is to create a small loop to create an entry

I've made a small Code snippit for you .
You can use it to create a Stored procedure, or you can change it to run Once.

Regards
Poor beggar


create table #Tmp
(
	[Name]  varchar(255),
	[Date] datetime
)


declare @start datetime;
declare @end datetime;
declare @Name varchar(255);

set @start = '1990-01-01' 
set @end = '2011-01-01'
set @name = 'John'


declare @LoopCounter int
declare @loops int
set @loops = convert(int,@end - @start)
set @LoopCounter=0

while @LoopCounter<=@loops
begin
	insert into #Tmp select @Name, dateadd(day,@LoopCounter,@start) 
	set @LoopCounter = @LoopCounter +1 
end 

select * from #Tmp
drop table #Tmp

Open in new window

0
 
LVL 18

Expert Comment

by:deighton
ID: 34886803
;WITH CTE
AS
(
	SELECT CAST('1900-01-01' AS DATETIME) AS GenDate 
	UNION ALL
	SELECT DATEADD(day,1,GENDATE) FROM CTE WHERE GENDATE < '2011-01-01'
)

SELECT * INTO DATETABLE FROM CTE
option (MAXRECURSION   0);			
	

Open in new window

0

Featured Post

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Calling stored proc in EDMX 11 19
SQL Inner Join Vs SubQueries 9 25
SQL Server creating a temp table 7 38
Birthdays 3 0
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now