Solved

Populate table with dates.

Posted on 2011-02-14
5
492 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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

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…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

830 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