Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8917425
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T05:28:49+00:00 2026-06-15T05:28:49+00:00

After hunting around on various forums for almost an hour, I’ve come to the

  • 0

After hunting around on various forums for almost an hour, I’ve come to the conclusion that SQL server is slightly stupid about simple arithmetic.

I am attempting to utilize a function which, until recently seemed to work just fine. Upon changing out some of the values for a different set of information on the form in use, I get the odd behavior ahead.

The problem is that it is giving me the incorrect result as based on an excel spreadsheet formula.

The formula looks like this:

=IF(D8=0,0,(((D8*C12-C16)*(100-C13)/100+C16)/D8)+(C18*D8))

My SQL looks like this:

(((@DaysBilled * @ContractRate - @ActualPlanDed) * (100 - @InsCover) / 100 + @ActualPlanDed) / @DaysBilled) + (@CoPay * @DaysBilled)

Filling the variables with the given data looks like this:

(((11 * 433 - 15) * (100 - 344) / 100 + 15) / 11) + (15 * 11)

Even stranger, if I use the numbers above (adding .00 to the end of each value) manually in the server environment, it gives me -11405.1200000000

With the values I am giving, it should come out 166.36. Unfortunately, I am getting -886.83

Here is the entire function and how it is called:

ALTER FUNCTION Liability
(
@ClientGUID CHAR(32),
@RecordGUID CHAR(32),
@Type CHAR(3)
)
RETURNS DECIMAL(18,2) AS
BEGIN

DECLARE @ReturnValue decimal(18,2);

    DECLARE @DaysBilled int;
    DECLARE @ContractRate decimal(18,2);
    DECLARE @ActualPlanDed decimal(18,2);
    DECLARE @InsCover decimal(18,2);
    DECLARE @CoPay decimal(18,2);

IF (@Type = 'RTC')
BEGIN   
        SELECT @DaysBilled = RTCDaysBilled, 
                @ContractRate = CAST(REPLACE(REPLACE(ContractRateRTC, ' ',''),'$', '') AS DECIMAL(6,2)),
                @ActualPlanDed = RTCActualPlanDed, 
                @InsCover = InsRTCCover, 
                @CoPay = RTCCoPay
        FROM AccountReconciliation1
        WHERE @ClientGUID = tr_42b478f615484162b2391ef0b2c35ddc
        AND @RecordGUID = tr_abb4effa0d9c4fe98c78cb4d2e21ba5d
END
IF (@Type = 'PHP')
BEGIN   
        SELECT @DaysBilled = PHPDaysBilled, 
                @ContractRate = CAST(REPLACE(REPLACE(ContractRatePHP, ' ',''),'$', '') AS DECIMAL(6,2)),
                @ActualPlanDed = PHPActualPlanDed, 
                @InsCover = InsPHPCover, 
                @CoPay = PHPCoPay
        FROM AccountReconciliation1
        WHERE @ClientGUID = tr_42b478f615484162b2391ef0b2c35ddc
        AND @RecordGUID = tr_abb4effa0d9c4fe98c78cb4d2e21ba5d
END
IF (@Type = 'IOP')
BEGIN   
        SELECT @DaysBilled = IOPDaysBilled, 
                @ContractRate = CAST(REPLACE(REPLACE(ContractRateIOP, ' ',''),'$', '') AS DECIMAL(6,2)),
                @ActualPlanDed = IOPActualPlanDed, 
                @InsCover = InsIOPCover, 
                @CoPay = IOPCoPay
        FROM AccountReconciliation1
        WHERE @ClientGUID = tr_42b478f615484162b2391ef0b2c35ddc
        AND @RecordGUID = tr_abb4effa0d9c4fe98c78cb4d2e21ba5d
END
        IF (@DaysBilled <> 0)
    BEGIN
        SET @ReturnValue = (((@DaysBilled * @ContractRate - @ActualPlanDed)
        *
        (100 - @InsCover) / 100 + @ActualPlanDed)
        /
            @DaysBilled
        )
        +
        (@CoPay * @DaysBilled)
    END
        ELSE
    BEGIN
        SET @ReturnValue = 0;
    END
RETURN @ReturnValue;
END

It is called by running a select statement from our front-end, but the result is the same as calling the function from within management studio:

SELECT dbo.Liability('ClientID','RecordID','PHP') AS Liability

I have been reading about how a unary minus tends to break SQL’s math handling, but I’m not entirely sure how to counteract it.

One last stupid trick with this function: It must remain a function. I cannot convert it into a stored procedure because it must be used with our front-end, which cannot utilize stored procedures.

Does SQL server even care about the parentheses? Or is it just ignoring them?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T05:28:50+00:00Added an answer on June 15, 2026 at 5:28 am

    The calculation is correct, it differes of course if you are using float values
    instead of integers.

    For (((11 * 433 – 15) * (100 – 344) / 100 + 15) / 11) + (15 * 11)
    a value around -886.xx depending in which places integers/floats are used is correct,
    What makes you believe it should be 166.36?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

After deploying WCF server (svc) on my Server, I have got this message when
After a system upgrade i am no longer able to start my tomcat5 server.
After I deployed WCF web service to Windows server 2008 R2, I was able
After a litte hunting through the docs I have decided on the following technique
After downloading and installing exuberant ctags, I get some issues hinting that the ctags
I am trying to create a secure chat server with python, and after many
After being frustrated in my attempts to learn the arcana that is ASP.NET, I
After a bit of investigation, I found that C++0x stores the elements in a
I've been hunting around for an answer on here, Google, etc., and can't seem
Today, after hunting a nasty bug, i tracked down the order of view controllers

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.