Iβm working on a recurring payment application. Payments are made once every two weeks.
- payment 1: 2009-06-01
- payment 2: 2009-06-15
- payment 3: 2009-06-29
and now I need an SQL statement that can calculate the next next payment date from the specified date in the WHERE clause
i.e. SELECT ... FROM ... WHERE someDate <[CALCULATION OF THE NEXT BOARD DATE FROM THIS DATE]
If I did this in C #, I would go
static DateTime CalculateNextPayDateFrom(DateTime fromDate) { var firstEverPayment = new DateTime(2009, 6, 1); var nextPayment = firstEverPayment; while (nextPayment < fromDate) { nextPayment += new TimeSpan(14, 0, 0, 0); } return nextPayment; }
So if i do
Console.WriteLine(CalculateNextPayDateFrom(new DateTime(2009, 6, 12)).ToString()); Console.WriteLine(CalculateNextPayDateFrom(new DateTime(2009, 6, 20)).ToString());
the conclusion will be
15/06/2009 12:00:00 am 29/06/2009 12:00:00 am
but I am completely stuck when I need to do this in SQL.
Can anybody help me? I am using SQL Server 2005
UPDATE: By the way, I forgot to mention that the last payment date is not available in the database, it must be calculated at runtime.
source share