0

I need to add date field column to the current table.

I need to add Jan1 to Dec31 of 2018 to the column named date.

I tried with following query,

 Insert into [dbo].[Calendar]  ([Date])
 Values
 Getdate()

It throws syntax error near getdate().

Please help me with the query.

krishna31
  • 113
  • 1
  • 9

3 Answers3

2

You can try below using recursive cte

DECLARE @startDt date='2018-01-01'
DECLARE @endtDt date='2018-12-31'    

;WITH GetDates As  
(  
SELECT 1 AS [COUNTER], @startDt as [Date]
UNION ALL  
SELECT [COUNTER] + 1, DATEADD(DAY,1,[Date])  
FROM GetDates  
WHERE [Date] < @endtDt   
)  
SELECT [DATE] FROM GetDates 
OPTION (MAXRECURSION 0)
Sreenu131
  • 2,476
  • 1
  • 7
  • 18
Fahmi
  • 37,315
  • 5
  • 22
  • 31
0

wrap getdate() inside braces.

Insert into [dbo].[Calendar]  ([Date])
 Values
 (Getdate())

And for adding Jan1 to Dec31 of 2018 use below code

declare @startDt date='2018-01-01'
declare @endtDt date='2018-12-31'
while (@startDt<=@endtDt)
begin
    Insert into [dbo].[Calendar]  ([Date]) Values (@startDt)
    set @startDt=dateadd(dd,1,@startDt)
end
0

Try this without using Recursive Cte

DECLARE @startDt date='2018-01-01'
DECLARE @endtDt date='2018-12-31'

SELECT DATEADD(DAY,Number,@startDt) AS RecurringDates
FROM Master.dbo.spt_values
WHERE [type] ='P'
AND DATEADD(DAY,Number,@startDt) BETWEEN @startDt AND @endtDt
ORDER BY 1
Sreenu131
  • 2,476
  • 1
  • 7
  • 18