Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Help with Scalar UDF

Author  Topic 

adamnmcc
Starting Member

2 Posts

Posted - 2010-11-19 : 04:43:34
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]
GO

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- 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)
AS

begin

declare @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 = @premium
else 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.

nigelrivett
Master Smack Fu Yak Hacker

3385 Posts

Posted - 2010-11-19 : 05:18:33
either datediff(day, @issuedate, @experiencedate)
or
datediff(day, @outdate, @experiencedate)
is 0 and being used to divide.
What do you want to do in that case.
Could be that it shouldn't happen but you have to decide whether your function should error or return a value

Of your variables only @earnedpremium is used.

==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

adamnmcc
Starting Member

2 Posts

Posted - 2010-11-19 : 05:34:50
ok, i'll have a play with that.

the other variables were from some testing i was doing with it.. seeing if it was the way i was calulating that was cocking it all up.

many thanks.
Go to Top of Page
   

- Advertisement -