SQL Scalar Function Help

What I need to accomplish is the following.
      I am passing in @TreatyNo, @IndemReserve from my stored procedure PXQ7000
      When there is a @TreatyNo from PXQ7000 it should be matched to the TreatyKey Field from dbo.treaty and have the following calculation applied.
      (@IndemReserve - Claims_Retention) * Treaty_Claims_PCT =      @IndemHPIXReserveCeded ELSE 0.00
      Claims_Retention & Treaty_Claims_PCT & TreatyKey are in dbo.Treaty





USE [EXPORTpahpixPROD]
GO
/****** Object:  UserDefinedFunction [dbo].[GetIndemHPIXReserveCeded]    Script Date: 09/24/2013 10:08:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
/********************************************************************************************************
                                                      GetIndemHPIXReserveCeded
                  
      Created      MJB      09/24/2013
            Gets the Indemnity HPIX Reserve calculation for the Loss Run

*********************************************************************************************************/
CREATE FUNCTION [dbo].[GetIndemHPIXReserveCeded]      (@TreatyNo VARCHAR(12),
                                                                   @IndemReserve NUMERIC(12, 2)
                                                                  )
                                                 
RETURNS NUMERIC(12,2)
 
AS  
BEGIN
      DECLARE @IndemHPIXReserveCeded NUMERIC(12,2)
            
      --SELECT
      
      --What I need to accomplish is the following.
      --I am passing in @TreatyNo, @IndemReserve from my stored procedure PXQ7000
      --When there is a @TreatyNo from PXQ7000 it should be matched to the TreatyKey Field from dbo.treaty and have the following calculation applied.
      --(@IndemReserve - Claims_Retention) * Treaty_Claims_PCT =      @IndemHPIXReserveCeded ELSE 0.00
      --Claims_Retention & Treaty_Claims_PCT & TreatyKey are in dbo.Treaty
      
      --FROM       dbo.Treaty
            
      RETURN @IndemHPIXReserveCeded
END
mburk1968Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Scott PletcherSenior DBACommented:
CREATE FUNCTION [dbo].[GetIndemHPIXReserveCeded]      (@TreatyNo VARCHAR(12),
                                                                   @IndemReserve NUMERIC(12, 2)
                                                                  )
                                                 
RETURNS NUMERIC(12,2)
 
AS  
BEGIN
RETURN (
    SELECT ISNULL(CAST((@IndemReserve - t.Claims_Retention) * t.Treaty_Claims_PCT AS numeric(12, 2)), 0.00)
    FROM dbo.Treaty t
    WHERE
        t.TreatyKey = @TreatyNo
    )
END
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mburk1968Author Commented:
This is perfect however I forgot to add one crucial element. I need to link to the State field as well however the only field I have to do this is my @Claim which looks like so 1-11-0039A or 5-10-0042B. I need the very first digit either a 1 or a 5

ALTER FUNCTION [dbo].[GetIndemHPIXReserveCeded]
    (
      @TreatyNo VARCHAR(12) ,
      @IndemReserve NUMERIC(12, 2) ,
      @Claim AS VARCHAR(20)
    )
RETURNS NUMERIC(12, 2)
AS
    BEGIN
        RETURN (
    SELECT ISNULL(CAST((@IndemReserve - t.Claims_Retention) * t.Treaty_Claims_PCT AS NUMERIC(12, 2)), 0.00)
    FROM dbo.Treaty t
    WHERE
        t.TreatyKey = @TreatyNo AND
        t.State = @Claim Just first digit
   )
    END
0
mburk1968Author Commented:
Nevermind I figured it out.
0
Scott PletcherSenior DBACommented:
Just in case :-) :

    WHERE
        t.TreatyKey = @TreatyNo AND
        t.State = LEFT(@Claim, 1)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.