Hi everyone,i've posted this ins r/rlearnprogramming too, but then i noticed this subreddit, can anyone help me?i've been tasked with moving creating a system in work and need to design a UDF that calculates the earned premium in insurance.the formula should be:if the experience date > return date premium earned = 100%if the experience date > issue date and < outward travel date premium earned = 20% / (datediff(experiencedate, issuedate)if the experience date is > outward travel date and < return date premium earned = 20% + (80% / (datediff(issuedate, outward traveldate))heres what i've got so far, but i keep getting divide by zero errors when i use the function in a query.====================================================USE [ATIUnderwriting]GOSET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGO-- =============================================-- Author: Adam McCartney-- Create date: 18/11/2010-- Description: calculate earned premium function-- =============================================ALTER FUNCTION [dbo].[fnEarnedPremium]( -- Add the parameters for the function here @issuedate datetime, @outdate datetime, @returndate datetime, @experiencedate datetime, @pretripearnings decimal(10,2), @premium decimal(10,2))RETURNS decimal(10,2)ASbegindeclare @earnedpremium decimal(10,2)declare @tripearnings decimal(10,2)--declare @tripearningsperc decimal(10,2)--declare @ptep decimal(10,2)declare @datediff decimal(10,2)if datediff(day, @experiencedate, @returndate) < 0 set @Earnedpremium = @premiumelse if datediff(day, @issuedate, @experiencedate) < datediff(day, @issuedate, @outdate) set @earnedpremium = @premium * (@pretripearnings / datediff(day, @issuedate, @experiencedate))Else set @earnedpremium = @premium * (@pretripearnings + (@tripearnings / datediff(day, @outdate, @experiencedate)))RETURN @earnedpremium end
====================================================any help would be fantastic.. i'm very new to programming. i now how to structure it but the syntax eludes me..Many Thanks in Advance.