Home > database >  Calculate future date based on date field
Calculate future date based on date field

Time:09-29

How I can calculate a date in the future based on the date of a field?

For example, I have a field in my dataset called SPS_START_DATE and I need to create a second field which is 20 weeks on from this date. What SQL would be required to calculate the date 20 weeks into the future?

CodePudding user response:

Anyway, use DATEADD

SELECT
         DATEADD(week, 20, [SPS_START_DATE])
    FROM
         [My DataSet];

CodePudding user response:

DATEADD() is perfect for this.

For example:

SELECT GETDATE() AS DateTime, --Get the date/time from today
CAST(GETDATE() AS DATE) AS DateOnly, --Cast date/time value as date only
DATEADD(wk, 20, GETDATE()) AS TwentyWeeksDateTime, --Get the date/time of 20 weeks from now
CAST(DATEADD(wk, 20, GETDATE()) AS DATE) AS TwentyWeeksDate --Cast date/time value as date only
  • Related