Insert all dates in a time period in a table

I have a table with two columns ( date_ID, entry_Date). I want to insert all dates in a specific time period into a table (for example, dates of all dates between 2002-2030). Is there a way to do this using loops in SQL Server?

+5
source share
4 answers

try it

DECLARE @d date='20020101' 
WHILE @d<'20300101'
    BEGIN
        INSERT INTO dbo.Dates (entry_Date)
        VALUES (@d)
        SET @d=DATEADD(DAY,1,@d)
    END
GO
+4
source

This should do it:

WITH TestItOut AS
(
    SELECT CAST('2002-01-01' as datetime) DateColumn
    UNION ALL
    SELECT DateColumn + 1
    FROM TestItOut
    WHERE DateColumn + 1 <= '2030-12-31'
)

INSERT INTO YourTable (ColumnName)
SELECT DateColumn
FROM TestItOut
OPTION (MAXRECURSION 0)
+4
source

insert into sometable 
select to_date('01/01/2013','dd/mm/yyyy') + level 
from dual 
connect by level < 10001

10000 1/1/13 . , , + level + level/24.

ANSI sql - SQL-.

+2
insert into table values(date_ID,(select entry_Date from table where entry_Date between 01/01/2002 and 01/01/2030))

.

_ID .

0

All Articles