>>>>Is it easy to do this as a computed column?
>>>
>>>Yes, of course. Using CASE expression and DATEADD fnction.
>>
>>Hi Naomi,
>>
>>I'm struggling a bit with this. I hope you or someone else has the time to give me a hand.
>>
>>I think what I need to do is create a scalar-valued function that returns the date and then add in a computed column to the table which calls this function. Is that right?
>>
>>My C# code gets a list of premiums paid and what the Premium Mode was for the payment, then loops through the result table adding months to the starting date. Here is where I get stuck. I don't know how to loop through a table in T-SQL, or if there is a better way. Any hints or help you can provide would be greatly appreciated.
>
>I see. You want to do something like Running Total, correct? If so, then leave the logic in C#
>
>T-SQL has some ideas for running totals (like Jeff Moden quirky update), but it's not official solution and not supported. So, for these kind of problems T-SQL may not be a good choice.
>
>Unless I misunderstood the problem.
>
>Perhaps you can post your table structure, what exactly do you want to get in SQL forum, then it will be easier to provide a solution.
Maybe this will get me the number of premiums paid:
SELECT NumberMonthsToAdd = SUM(
CASE [PremiumMode]
WHEN 'Monthly' THEN NumberOfPremiums * 1
WHEN 'Quarterly' THEN NumberOfPremiums * 3
WHEN 'Semi-Annually' THEN NumberOfPremiums * 6
WHEN 'Annually' THEN NumberOfPremiums * 12
ELSE 0
END )
FROM [dbo].[ReceiptDetails]
This way I don't need to loop through to get the result. What do you think?