# Write date records to a table.

I have table A with 4 records and each record contains 1 field that has either 1,2,3 or 4 in it:
1
2
3
4

I have table B:

When I read table A and Table A's record has a 1:
Write 1 record to Table B with todays date.

When I read table A and Table A's record has a 2:
Write 1 record to Table B with tomorrows date.

When I read table A and Table A's record has a 3:
Write 7 records to Table B which will represent the next 7 days starting with tomorrows date.

When I read table A and Table A's record has a 4:
Write 365 records to Table B which will represent  365 days starting with tomorrows date + 14 days..

Date format would be 2015-11-16
###### Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
Variable @val decides the case
``````DECLARE @val int = 4
DECLARE @start date
DECLARE @end date
DECLARE @currentDate date

SET @currentDate = CONVERT(date,GETDATE())

SELECT @start =  CASE WHEN @val = 1 THEN @currentDate
ELSE DATEADD(D , 1 , @currentDate)
END

SELECT @end =    CASE WHEN @val = 1 THEN @currentDate
WHEN @val = 2 THEN DATEADD(D , 1 , @currentDate)
WHEN @val = 3 THEN DATEADD(D , 8 , @currentDate)
WHEN @val = 4 THEN DATEADD(D , 366 , @currentDate)
END

SELECT @start, @end;

;with dateRange as
(
select dt = dateadd(dd, 0, @start)
where dateadd(dd, 0, @start) <= @end
union all
select dateadd(dd, 1, dt)
from dateRange
where dateadd(dd, 1, dt) < @end
)
select *
from dateRange
OPTION (MAXRECURSION 365)
``````
Programmer AnalystAuthor Commented:
I have you code working but having a issue.

Your code is inside a stored procedure that is used to create report.

Your code uses SELECT inside the WHILE statements. These SELECT' s create results that show up when I execute  my stored procedure. These SELECT results interfere with my final Select query in my Store Procedure that feed the SSRS report.

Is there any way to do your coding where you don't use SELECT or is there a way to delete them so my final SELECT result is the only SELECT my SSRS gets ?
Commented:
In place of select use SET to set variable value
Commented:
``````DECLARE @val int = 4
DECLARE @start date
DECLARE @end date
DECLARE @currentDate date

SET @currentDate = CONVERT(date,GETDATE())

SET @start =  (SELECT CASE WHEN @val = 1 THEN @currentDate
ELSE DATEADD(D , 1 , @currentDate)
END)

SET @end = (SELECT   CASE WHEN @val = 1 THEN @currentDate
WHEN @val = 2 THEN DATEADD(D , 1 , @currentDate)
WHEN @val = 3 THEN DATEADD(D , 8 , @currentDate)
WHEN @val = 4 THEN DATEADD(D , 366 , @currentDate)
END)

;with dateRange as
(
select dt = dateadd(dd, 0, @start)
where dateadd(dd, 0, @start) <= @end
union all
select dateadd(dd, 1, dt)
from dateRange
where dateadd(dd, 1, dt) < @end
)
``````

And join/select.. what ever is required to do with dateRange

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Programmer AnalystAuthor Commented:
Works perfect now.
Does exactly what I need.
Thanks for your help and quick responses.
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.