Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Date Manipulation
Message
De
06/08/2018 11:44:23
Cetin Basoz
Engineerica Inc.
Izmir, Turquie
 
 
À
06/08/2018 09:10:42
Information générale
Forum:
Microsoft SQL Server
Catégorie:
Scripting
Versions des environnements
SQL Server:
SQL Server 2012
Divers
Thread ID:
01661461
Message ID:
01661468
Vues:
39
>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?

Probably TSQL version wouldn't even be close to such a code. But before suggesting some TSQL, would you make it a little more clear.

Say m.pdService is claim date, m.tdPeriodStart is meant to be Day and Month of period start (Feb 1st), and we have period as months. Find out in which period m.pdService is falling, right?

I think it works like that:
DECLARE @claimDate DATE = '20180101';
DECLARE @periodStart DATE = '20100201';
DECLARE	@period INT = 12; 

DECLARE @pStart DATE = DATEFROMPARTS(YEAR(@claimDate),MONTH(@periodStart), DAY(@periodStart));

SELECT CASE WHEN @claimDate BETWEEN @pStart AND DATEADD(m,@period,@pStart)
THEN @pStart
ELSE DATEADD(YEAR,-1,@pStart)
END;
Actually I would do the VFP code alike:
Local claimDate, periodStart, period, pStart
m.claimDate = Date(2018,01,01)
m.periodStart = Date(2010,02,01)
m.period = 12 

m.pStart = DATE(YEAR(m.claimDate),MONTH(m.periodStart), DAY(m.periodStart))

if !Between(m.claimDate, m.pStart, Gomonth(m.pStart, m.period))
   m.pStart = Gomonth(m.pStart,-12)
endif
? m.pStart
Çetin Basöz

The way to Go
Flutter - For mobile, web and desktop.
World's most advanced open source relational database.
.Net for foxheads - Blog (main)
FoxSharp - Blog (mirror)
Welcome to FoxyClasses

LinqPad - C#,VB,F#,SQL,eSQL ... scratchpad
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform