Logo

SQL Server PRODUCT38 Function

Updated 2023-10-13 20:50:17.537000

Description

Use the scalar function PRODUCT38 to return the product of 2 decimal(38,18) values as a decimal with precision 38 and scale 18. Numeric is functionally equivalent to decimal.

Syntax

SELECT [wctMath].[wct].[PRODUCT38](
  <@Val1, numeric(38,18),>
 ,<@Val2, numeric(38,18),>)

Arguments

@Val1

is the first value in the multiplication. @Val1 is an expression of type decimal(38,18) or of a type that can be implicitly converted to decimal(38,18).

@Val2

is the second value in the multiplication. @Val2 is an expression of type decimal(38,18) or of a type that can be implicitly converted to decimal(38,18).

Return Type

numeric(38,18)

Remarks

The maximum value returned by the function is 9999999999999999999.999999999999999999.

The minimum value returned by the function is -9999999999999999999.999999999999999999.

If the product of @Val1 and @Val2 is greater than the maximum value or less than the minimum value, a NULL will be returned.

For division, use the QUOTIENT38 function.

Examples

This example demonstrates the difference in results between standard decimal multiplication in SQL Server and the PRODUCT38 function.

CREATE TABLE #p
(
    rn int,
    val1 decimal(38, 2),
    val2 decimal(38, 18)
);
INSERT INTO #p
SELECT *
FROM
(
    VALUES
        (1, 3142378999.94000000000, 0.869991702044623665),
        (2, 4090438446.96000000000, 0.315926812205999074),
        (3, -8982295492.57000000000, 0.145070738816429198),
        (4, 2206701138.82000000000, 0.477841849612603496),
        (5, 2536384471.87000000000, 0.333725266533017807),
        (6, -9407920469.55000000000, 0.384890656235674581),
        (7, 6248792872.11000000000, 0.827245991552436995),
        (8, -2387385424.14000000000, 0.532558374599493736),
        (9, 1080470114.09000000000, 0.036180548112184794),
        (10, 3792417107.80000000000, 0.569700878077553619)
) n (rn, val1, val2);
SELECT *,
       val1 * val2 as [SQL Multiply],
       wct.PRODUCT38(Val1, val2) as PRODUCT38
FROM #p;
DROP TABLE #p;

This produces the following result.

rnval1val2SQL MultiplyPRODUCT38
13142378999.940.8699917020446236652733843654.6270832733843654.627082965676357580
24090438446.960.3159268122059990741292279179.0729301292279179.072930423847758115
3-8982295492.570.145070738816429198-1303068243.374612-1303068243.374611721857940059
42206701138.820.4778418496126034961054454153.7159871054454153.715987310448313315
52536384471.870.333725266533017807846455583.905023846455583.905023356325000589
6-9407920469.550.384890656235674581-3621020683.338135-3621020683.338135239542519509
76248792872.110.8272459915524369955169288855.4944385169288855.494437567655867709
8-2387385424.140.532558374599493736-1271422101.022521-1271422101.022521355549633187
91080470114.090.03618054811218479439092000.94661139092000.946611038492343147
103792417107.800.5697008780775536192160543356.3499962160543356.349996319867403128