Home > database >  Have a start date and the number of days, according to 30 days to calculate the final date
Have a start date and the number of days, according to 30 days to calculate the final date

Time:11-15

Start date: 2020-06-30 days: 45 end date: 2020-08-15, language sentence convert (varchar (10), DATEADD (day, 45, '2020-06-30'), 120) only to the 2020-08-14, so no, calculation method,

CodePudding user response:

What is a "calculated on 30 days"?

CodePudding user response:

According to 30 days per month, 12 months is 360 days, there are 31 also calculate for 30 days

CodePudding user response:

 
The create function dbo. Dateadd2 (@ the date date, @ add int)
Returns the date
As
The begin
Declare @ date2 date, @ dt int

Select @ dt=(select count (1)
From the master. The dbo. Spt_values
Where type=N 'P'
And number<=abs (@ add)
And right (convert (varchar, dateadd (dd, number * iif (@ add>=0, 1, 1), @ the date), 23), 2)='31')

Select @ date2=dateadd (dd, @ add + @ dt * iif (@ add>=0, 1, 1), @ date)

Return the @ date2
End


The select date2=dbo. Dateadd2 (' 2020-06-30 ', 45)

/*
Date2
-- -- -- -- -- -- -- -- -- --
The 2020-08-15

Line (1) affected
*/

CodePudding user response:

You will still be some problems, such as 2020.02.29, plus 360 days, but no on February 29, 2021, it will still be different from your request results,
 
DECLARE @ DT DATE
DECLARE @ DIFF INT

The SET @ DT='2020-02-28'
The SET @ DIFF=10



The SELECT DATEADD (DAY, (DAY @ (DT) + (% @ DIFF 30)) % 1, 30 DATEADD (MONTH, (@ DIFF/30) + (DAY @ (DT) + (30) % @ DIFF)/30, DATEADD (DAY, 1 * DAY (@ DT) + 1, @ DT)))
  • Related