0

Example:

-- inputs
declare @x decimal(28,10) = 10001.0 
declare @y decimal(18,6) = 7.0

-- later on, inside a udf
select POWER(@x, @y)

Result:

Msg 8115, Level 16, State 6, Line 13
Arithmetic overflow error converting float to data type numeric.

I understand why the overflow is occurring. My question is, is it possible to detect, just before POWER is executed, whether the overflow would occur? Note that the code is run inside a UDF, so cannot use TRY...CATCH. If I can detect it in advance, I can take avoiding action (e.g. return NULL for the result, which is suitable for my requirements).

7
  • 1
    Sure, you know the size that causes an overflow, and you know the inputs that might cause that... so go ahead and test for them.
    – Dale K
    Commented Feb 7, 2022 at 0:39
  • sorry, i should have clarified - the values of @x and @y can differ for each call to the udf (although the precision and scale of @x and @y will always be the same). So I'd like the check to work for (ideally) all possible combinations of @x and @y values.
    – Moe Sisko
    Commented Feb 7, 2022 at 0:44
  • 1
    My comment still stands... there is no magic way to find out whether the result will overflow.
    – Dale K
    Commented Feb 7, 2022 at 0:45
  • ok, thanks Dale.
    – Moe Sisko
    Commented Feb 7, 2022 at 0:45
  • 1
    Side point: you should probably convert your UDF to an inline TVF for performance reasons Commented Feb 7, 2022 at 1:04

1 Answer 1

3

You could use Try-Catch or you could use a formula to predict the output digits and return NULL instead. Formula to predict number of digits from here

Predict Number of Digits of Power Function

Declare @Num DECIMAL(28,10) = 10001
    ,@Exponent DECIMAL(28,10) = 7
    ,@NumOfDigits INT

/*Predict number of digits from power function*/
SELECT @NumOfDigits = FLOOR(1 + @exponent* CAST(LOG(@Num,10) AS DECIMAL(38,10)))

SELECT 
    CASE WHEN @NumOfDigits <= 38 /*Max decimal precision, return type from POWER function according to https://learn.microsoft.com/en-us/sql/t-sql/functions/power-transact-sql?view=sql-server-ver15*/ 
              - 10 /*Scale of @Num. Need to leave enough digits to record decimal places*/
        THEN POWER(@Num,@Exponent) /*If less than or equal to precision, return value*/
    ELSE NULL /*If outside precision, just returns NULL. Could update to return something else*/
END
3
  • thanks, this looks promising. But when Num = 10000, Exponent = 7, this code returns NULL, whereas POWER(Num, Exponent) for those values would not throw an error. Any ideas ?
    – Moe Sisko
    Commented Feb 7, 2022 at 2:31
  • 1
    So I think my function works, but SQL Server is doing the math "wrong" because the POWER function use floating point math, which has inaccuracies (many articles online detailing floating point math inaccuracies). You can tell because 10,000^7 is 1 with 28 zeros after it. That would be 29 digits + 10 digits after decimal = 39 digits, which should exceed DECIMAL's max precision, so my function returns NULL. But if you manually run, SELECT POWER(CAST(10000 AS DECIMAL(38,10)),7), you'll notice the answer is close, but not right at 9999999999999999583119736832
    – Stephan
    Commented Feb 7, 2022 at 3:57
  • Unfortunately, I don't think there is anything you can really do as this is an inherent limitation of the POWER function. Only thing you maybe could do is create your own version of the POWER function, using something like a recursive CTE. I'd expect performance would be negatively impacted though
    – Stephan
    Commented Feb 7, 2022 at 4:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.