1
Answer

How to create date column automatically

madhu goud

madhu goud

9y
380
1
This is my code sir
 
create table demotable
(
Id int primary key identity(1,1),
Name varchar(50),
StartDate date,
EndDate date
)
declare @command nvarchar(max)
declare @dates int, @StartDate datetime, @EndDate datetime
---Here, we assume that,
Select @StartDate = convert(datetime, '1/7/2015'), @endDate = convert(datetime,'1/10/2015')
select @dates = datediff (dd,@StartDate ,@Enddate)
While (@dates >0) begin
set @command = 'alter table demotable add [1/7/2015' + cast(@dates as nvarchar(max)) + '] datetime'
exec sp_executesql @command
select @dates = @dates - 1
end
 
 
 
when i execute this am getting the output is
 Id    Name            StartDate    EndDate      1/7/20153       1/7/20152      1/7/20151
 
 
but i want date columns like 1/7/2015   1/8/2015   1/9/2015 
I want to increase date columns using start date and end date  
Answers (1)