Logo

PRODUCT38

Updated 2023-10-13 20:50:17.537000

Syntax

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

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.

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.

{"columns":[{"field":"rn","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"val1","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"val2","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"SQL Multiply","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"PRODUCT38","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"}],"rows":[{"rn":"1","val1":"3142378999.94","val2":"0.869991702044623665","SQL Multiply":"2733843654.627083","PRODUCT38":"2733843654.627082965676357580"},{"rn":"2","val1":"4090438446.96","val2":"0.315926812205999074","SQL Multiply":"1292279179.072930","PRODUCT38":"1292279179.072930423847758115"},{"rn":"3","val1":"-8982295492.57","val2":"0.145070738816429198","SQL Multiply":"-1303068243.374612","PRODUCT38":"-1303068243.374611721857940059"},{"rn":"4","val1":"2206701138.82","val2":"0.477841849612603496","SQL Multiply":"1054454153.715987","PRODUCT38":"1054454153.715987310448313315"},{"rn":"5","val1":"2536384471.87","val2":"0.333725266533017807","SQL Multiply":"846455583.905023","PRODUCT38":"846455583.905023356325000589"},{"rn":"6","val1":"-9407920469.55","val2":"0.384890656235674581","SQL Multiply":"-3621020683.338135","PRODUCT38":"-3621020683.338135239542519509"},{"rn":"7","val1":"6248792872.11","val2":"0.827245991552436995","SQL Multiply":"5169288855.494438","PRODUCT38":"5169288855.494437567655867709"},{"rn":"8","val1":"-2387385424.14","val2":"0.532558374599493736","SQL Multiply":"-1271422101.022521","PRODUCT38":"-1271422101.022521355549633187"},{"rn":"9","val1":"1080470114.09","val2":"0.036180548112184794","SQL Multiply":"39092000.946611","PRODUCT38":"39092000.946611038492343147"},{"rn":"10","val1":"3792417107.80","val2":"0.569700878077553619","SQL Multiply":"2160543356.349996","PRODUCT38":"2160543356.349996319867403128"}]}