Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Date Manipulation
Message
 
To
06/08/2018 09:10:42
General information
Forum:
Microsoft SQL Server
Category:
Scripting
Environment versions
SQL Server:
SQL Server 2012
Miscellaneous
Thread ID:
01661461
Message ID:
01661487
Views:
38
>Hi,
>I have a situation where an Insurance Plan can start on a user defined date (say 1st February 2010) and it has a period length of a user defined number of months (say 12 months).
>If a user makes a claim in March 2018, the start of the relevant period is 1st February 2018. Or if the claim was on 10th January 2018, the start of the relevant period would be 1st Feb 2017.
>
>I worked out this start date with this code in Visual FoxPro:
>
>
			DO WHILE m.pdService > GOMONTH(m.tdPeriodStart, m.tnPeriodLength)
>				m.tdPeriodStart = GOMONTH(m.tdPeriodStart, m.tnPeriodLength)
>			ENDDO
>
>I am now trying to write a Stored Procedure which has to calculate this date, is there any easy way to do this in TSQL? Or shall I just apply a similar LOOP and keep adding the period Length to the start date?
DECLARE @dStartPeriod datetime
DECLARE @PeriodLength int
DECLARE @ClaimDate datetime

SET @dStartPeriod = '20100201'
SET @PeriodLength = 12
SET @ClaimDate ='20180110'
---SET @ClaimDate ='20180301'


;WITH CteCounter (amnt)
AS
(SELECT @dStartPeriod AS Amnt
 UNION ALL
 SELECT DATEADD(mm,@PeriodLength, Amnt) AS Amnt
 FROM CteCounter
 WHERE  DATEADD(mm,@PeriodLength, Amnt) < @ClaimDate)
 
 
 SELECT TOP 1 * FROM CteCounter ORDER BY Amnt DESC
Against Stupidity the Gods themselves Contend in Vain - Johann Christoph Friedrich von Schiller
The only thing normal about database guys is their tables.
Previous
Reply
Map
View

Click here to load this message in the networking platform