首页 > 解决方案 > 如何限制 SQL Server 函数舍入 Money 返回值

问题描述

我做了一个函数来获取带有返回值的转换率,但是为什么 sql server 对数据输出进行四舍五入。

我在 SQL Server 上做了一个标量函数,返回输出资金

CREATE FUNCTION CurrencyRate(
-- Add the parameters for the function here
@FromCurr  CHAR(3), 
@ToCurr    CHAR(3), 
@Date_Tran DATE)
RETURNS MONEY
AS
     BEGIN
         -- Declare the return variable here
         DECLARE @CurrencyRate MONEY;

         -- the T-SQL statements to compute the return value here
         SELECT @CurrencyRate = CAST(tc.scale AS MONEY)
         FROM dbo.TCurrencyConverter tc
         WHERE @Date_Tran BETWEEN tc.start_date AND tc.end_date
               AND tc.currency_id_1 = @FromCurr
               AND tc.currency_id_2 = @ToCurr;

         -- Return the result of the function
         RETURN @CurrencyRate;
     END;
GO

我希望输出是0.000070550000000,但实际输出是0.0001

标签: sqlsql-serverfunction

解决方案


这是货币数据类型的最大sqlserver大小

92233720368547.9999

您可以使用 numeric() 作为返回类型。

cast('0.000070550000000' as numeric(18,18))

推荐阅读