Click here to Skip to main content
15,914,409 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hi Have the following records in my table:


CSS
DateId  Date                      InstId
1       2013-09-01 00:00:00.000      1
2       2013-09-09 00:00:00.000      1
3       2013-09-01 00:00:00.000      1
4       2013-09-08 00:00:00.000      1
5       2013-09-15 00:00:00.000      2
6       2013-09-22 00:00:00.000      2
7       2013-09-01 00:00:00.000      3
8       2013-09-15 00:00:00.000      3
9       2013-09-29 00:00:00.000      3


Now what I want is to get the difference between two consecutive Dates and compare if the difference is less then 30 days or not where InstId is same.
example
-if Date2 - Date1 > 30 then do something
-if Date3 - Date2 > 30 the do something, etc...

How to perform this, using a stored procedure?
Posted
Comments
Azee 28-Sep-13 11:18am    
Could you please specify the possible output that you would expect from the stored procedure?
El Dev 28-Sep-13 11:31am    
I want to charge an interest to customers if the difference between 2 dates exceed 30 days...means if I give a customer a loan of $300 to be paid in 3 installment for $100,if the customer exceed 30 days he will star paying interest of $10 for each 30 days.

1 solution

I am not sure how you would wanna use it but here are two different scenario to use the days difference.
The first one gets you the Days difference between consecutive records with same IntsId
SQL
select ISNULL(DATEDIFF(dd,M1.Date, (Select [Date] from MyTable where dateId = M1.dateId + 1 and InstId = M1.InstId)), 0) as diff from MyTable M1

The second one checks if there are any consecutive records with same InstId with difference greater than 30.
SQL
select 'Charge 10%' as Fine  from MyTable M1
where  ISNULL(DATEDIFF(dd,M1.Date, (Select [Date] from MyTable where dateId = M1.dateId + 1 and InstId = M1.InstId)), 0) > 30


Hope it helps.

Good luck
 
Share this answer
 
Comments
El Dev 28-Sep-13 13:27pm    
Thanks Asif!!!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900