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
 General SQL Server Forums
 New to SQL Server Programming
 Insert multiple rows between dates

Author  Topic 

Gaillimh
Starting Member

3 Posts

Posted - 2011-05-17 : 09:30:48
Hi,

I have a start date and an end date:

$start_date = '2011-05-01'
$end_date = '2011-05-04'

How can I get SQL insert four records between those dates so that my table looks like this:

id | date
---------------
1 | 2011-05-01
2 | 2011-05-02
3 | 2011-05-03
4 | 2011-05-04



Any tips greatly appreciated.

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2011-05-17 : 09:42:52
[code]DECLARE @start_date DATETIME; SET @start_date = '2011-05-01';
declare @end_date DATETIME; SET @end_date = '2011-05-04';

DECLARE @days INT SET @days = DATEDIFF(dd, @start_date,@end_date)+1;

;WITH N(n) AS (SELECT 1 UNION ALL SELECT n+1 FROM N WHERE n < @days)
SELECT
n,DATEADD(dd,n,@start_date)
FROM
N[/code]
Go to Top of Page

jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2011-05-17 : 09:44:04
SELECT DISTINCt DATEADD(day, number,date)
FROM master..spt_values
CROSS JOIN @table
where [type] = 'p' and DATEADD(day, number,date) <=
(select max(date) from @table)


Jim

Everyday I learn something that somebody else already knew
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2011-05-17 : 09:52:54
use F_TABLE_DATE


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2011-05-17 : 09:56:49
Why do you want to create skeleton rows?



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page
   

- Advertisement -