add 12 weeks to date

Neil 396 Reputation points
2021-02-16T22:48:10.053+00:00

I want to add 12 weeks to the date column as below using sql query

If dateSt has value Feb 04, 2021 the DateEd should be April 29, 2021
If dateSt has value Feb 14, 2021 the DateEd should be May 09, 2021
If dateSt has value Feb 26, 2021 the DateEd should be May 21, 2021

Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,656 questions
0 comments No comments
{count} votes

Accepted answer
  1. EchoLiu-MSFT 14,581 Reputation points
    2021-02-17T02:00:00.99+00:00

    Hi @Neil ,

    Please try:

        CREATE TABLE #test(dateSt date)  
        INSERT INTO #test values('20210204'),('20210214'),('20210226')  
          
        SELECT dateSt,dateadd(week,12,dateSt) as DateEd  
        FROM #test  
    

    Output:

        dateSt DateEd  
        2021-02-04 2021-04-29  
        2021-02-14 2021-05-09  
        2021-02-26 2021-05-21  
    

    If you have any question, please feel free to let me know.

    Regards
    Echo


    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

4 additional answers

Sort by: Most helpful
  1. Jeffrey Williams 1,896 Reputation points
    2021-02-16T23:18:18.503+00:00
    Select dateadd(day, 12*7, '20210204')
         , dateadd(day, 12*7, '20210214')
         , dateadd(day, 12*7, '20210226');
    

    This?

    0 comments No comments

  2. Neil 396 Reputation points
    2021-02-17T12:46:54.657+00:00

    Thanks Jeffrey and Liu, both the solution looks good

    0 comments No comments

  3. Neil 396 Reputation points
    2021-02-17T15:44:56.617+00:00

    I tried this first
    SELECT dateSt,dateadd(week,12,dateSt) as DateEd

    and it is working as expected.

    I sampled the result of other soln as well, I am getting the same results for both the solution. However, I implemented dateadd(week,12,dateSt) as DateEd, considering the simplicity.

    But yes this solution is also working as expected and thank you for the solution.
    Select dateadd(day, 12*7, '2021-02-05')

    0 comments No comments

  4. Jeffrey Williams 1,896 Reputation points
    2021-02-17T21:30:04.913+00:00

    The solution using days was really just to show a different method - and to also show that dateadd(week is really just 'dateadd(day, 7 * #weeks'.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.