Anju Muralidharan
Anju Muralidharan

Reputation: 647

How to find date after some periods of days

I am using SQL Server 2008. I want to get date from current date and some input days.

For example: periods in days = 300 (may be any days as user need)

Current date

I tried this:

ALTER PROCEDURE [dbo].[SCHEME_DETAILS_SUBCRIBE]
    @PERIOD varchar(50)
AS
BEGIN
    SET NOCOUNT ON;

    INSERT INTO [USER_SUBCRIPTION_DETAILS] ([USER_SUB_DATE], [USER_SUB_END_DATE])
    VALUES( ,GETDATE(), DATEADD(DAY, @PERIOD, GETDATE()) )
 END 

How will get the date between these two?

Upvotes: 0

Views: 192

Answers (3)

Abhishek
Abhishek

Reputation: 2490

This will be another way to accomplish if you are passing period in days directly -

;with cte as (
select getdate() as [urdate], 1 as Level
union all
select dateadd(dd,1,urdate), level + 1
from cte where level < 300 -- here the 300 can vary based on your input/ user input
)
select cast(urdate as date) from cte option ( MaxRecursion 0 )

Edit: If you want only the end date then this should do for you -

;with cte as (
select getdate() as [urdate]
union all
select dateadd(dd,300,getdate()) -- this is the main statement and 300 will be configurable in your case
)
select cast(urdate as date) from cte

Upvotes: 0

Sateesh Pagolu
Sateesh Pagolu

Reputation: 9606

This is one way of doing it

DECLARE @Period INT = 10 --300 in your case
DECLARE @FromDate DATE = GETDATE()
DECLARE @ToDate DATE = DATEADD(dd,@Period,@fromDate)

;WITH cte AS
(
    SELECT @fromDate AS dt
    UNION ALL
    SELECT DATEADD(dd,1,dt) FROM cte WHERE dt<@ToDate
)

SELECT * FROM cte

Update: If you only want enddate after period number of days, here it is

DATEADD(dd,@Period,@fromDate)

Note, period must be of type int

Upvotes: 0

SQL_Deadwood
SQL_Deadwood

Reputation: 521

In order to achieve this use the DATEADD() function. You will then need to find a way to pass the date parts as variables.

Regarding your posted code... What happens when you run that?

Upvotes: 0

Related Questions