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 data by incrementing values

Author  Topic 

mavericky
Posting Yak Master

117 Posts

Posted - 2011-11-06 : 16:22:53
Hi ,
I have a table which has date columns
StartDT
EndDT
where I want to insert data.

There is a series starting from
StartDT EndDT
2011-09-12 00:10:00.000 2011-09-12 00:11:00.000
2011-09-12 00:11:00.000 2011-09-12 00:12:00.000
.
. so on till
2011-09-15 00:11:00.000 2011-09-15 00:12:00.000

Is there any way I can do it faster through code, rather than manually inserting it?
Thanks,
Mavericky

malpashaa
Constraint Violating Yak Guru

264 Posts

Posted - 2011-11-06 : 20:03:27
Try this:

DECLARE @start_date DATETIME = '20110912 00:10:00.000';
DECLARE @end_date DATETIME = '20110915 00:12:00.000';

WITH DigitsCTE AS
(
SELECT digit
FROM (VALUES(0), (1), (2), (3), (4), (5), (6), (7), (8), (9)) AS D(digit)
)
, NumbersCTE AS
(
SELECT TOP(DATEDIFF(MINUTE, @start_date, @end_date))
ROW_NUMBER() OVER(ORDER BY(SELECT NULL)) AS number
FROM DigitsCTE AS D0,
DigitsCTE AS D1,
DigitsCTE AS D2,
DigitsCTE AS D3
)
SELECT
DATEADD(MINUTE, number - 1, @start_date) AS StartDT,
DATEADD(MINUTE, number, @start_date) AS EndDT
FROM NumbersCTE;
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-11-07 : 04:28:06

see similar logic here

http://visakhm.blogspot.com/2010/02/generating-calendar-table.html


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -