SQL Server 中的 *UP* 舍入到最接近的 100

发布于 2024-09-08 12:13:29 字数 130 浏览 6 评论 0原文

在 SQL Server 中是否可以轻松地将数字向上舍入到最接近的 100(或 1000、500、200 等)?

所以:

720 -> 800
第790章800
第1401章1500

Is it possible to easily round a figure up to the nearest 100 (or 1000, 500, 200 etc.) in SQL Server?

So:

720 -> 800
790 -> 800
1401 -> 1500

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(17

草莓味的萝莉 2024-09-15 12:13:30

这对我来说效果很好。

回合(@value/100, 0) * 100

This worked fine for me.

Round(@value/100, 0) * 100

千年*琉璃梦 2024-09-15 12:13:30
Select round(value/100,0)*100

无论您想要四舍五入到什么数字,只需在此处发布该数字即可,而不是 100

Select round(value/100,0)*100

Whatever number you want to round to, just post that instead of 100 here.

最后的乘客 2024-09-15 12:13:30

我在mssql中创建了一个函数它可以帮助你

创建函数 dbo.roundup
(
@numbr 小数(18,2),
@frac 小数(18,2)

返回十进制(18,2)
作为
开始
声明@结果十进制(18,2)
设置@结果 = 上限(@numbr/@frac)*@frac
返回@结果
结尾

i have create a function in mssql it can help you

CREATE function dbo.roundup
(
@numbr decimal(18,2),
@frac decimal(18,2)
)
RETURNS decimal(18,2)
AS
BEGIN
DECLARE @result decimal(18,2)
set @result = ceiling(@numbr/@frac)*@frac
RETURN @result
END
GO

避讳 2024-09-15 12:13:30

试试这个代码

round(yourvalue , -2)

解释=>当我们在舍入函数中提到 -ve 时,它​​会对值进行四舍五入并使其为零
示例 =>round(1238283.9827398 ,-2) 给出输出 =>1238300.0000000

try this code

round(yourvalue , -2)

Explaination=> when we mentioned -ve in round funtion it rounds the values and make zero
Example=>round(1238283.9827398 ,-2) gives output=>1238300.0000000

西瓜 2024-09-15 12:13:30
  • SELECT ROUND(Number,-2) 用于舍入最接近的第 100 个位置
  • SELECT ROUND(Number,-3) 用于舍入最接近的第 100 个位置
  • SELECT ROUND(Number,-2) for rounding nearest hundredth position
  • SELECT ROUND(Number,-3) for rounding nearest thousandth position
皇甫轩 2024-09-15 12:13:29

以下应该有效。读完你的问题后,我不太确定你想要 100 返回什么。对于此 100 返回 100。

select floor((X + 99) / 100) * 100;

这给出以下结果:

0 -> 0
1 -> 100
99 -> 100
100 -> 100
101 -> 200

The following should work. After reading your question, I'm not exactly sure what you want 100 to return. For this 100 returns 100.

select floor((X + 99) / 100) * 100;

This gives the following results:

0 -> 0
1 -> 100
99 -> 100
100 -> 100
101 -> 200
离线来电— 2024-09-15 12:13:29

要四舍五入到最接近的千位,请尝试以下操作:-

select round(YourValue, -3)

For rounding Up to the nearest thousand, try the following:-

select round(YourValue, -3)
吻风 2024-09-15 12:13:29

一种选择是使用 CEILING() 函数,如下所示

SELECT CEILING(@value/100.0) * 100

:可能需要首先将您的值转换为小数,具体取决于其类型。

One option would be to use the CEILING() function like this:

SELECT CEILING(@value/100.0) * 100

You may need to convert your value to a decimal first depending on its type.

鱼忆七猫命九 2024-09-15 12:13:29

使用 CEILING 函数对数字进行四舍五入

DECLARE @Number DECIMAL, @RoundUp DECIMAL
SET @RoundUp = 100
SET @Number = 720
SELECT CEILING(@Number/@RoundUp)*@RoundUp

Use CEILING function to round a figure up

DECLARE @Number DECIMAL, @RoundUp DECIMAL
SET @RoundUp = 100
SET @Number = 720
SELECT CEILING(@Number/@RoundUp)*@RoundUp
情感失落者 2024-09-15 12:13:29

试试这个:

选择回合(@value,-2);

Try this:

select round(@value , -2);

小清晰的声音 2024-09-15 12:13:29

只需使用 ROUND 函数即可非常简单地将数字舍入为最接近 10 的任意倍数
例如:

SELECT ROUND(number/1000,2)*1000 

这将为您提供最接近的千分之一值。

It is very simple to round a number to any multiple of nearest 10 by using simply the ROUND function
for ex:

SELECT ROUND(number/1000,2)*1000 

This will give you the nearest thousandth value.

往事风中埋 2024-09-15 12:13:29

这也适用于带小数的值。

选择楼层((ceiling (@value) + 99) / 100) * 100;

This will work for the values with decimal also.

select floor((ceiling (@value) + 99) / 100) * 100;

不知所踪 2024-09-15 12:13:29

它适用于整数值:

@roundUpValue = ((@intValue / 1000) + 1) * 1000
@roundDownValue = (@intValue / 1000) * 1000

例如,

declare @intValue as int = 1934
select ((@intValue / 1000) + 1) * 1000 as roundUp
select (@intValue / 1000) * 1000 as roundDown

如果您想向上舍入到最接近的 500,则

select ((@intValue / 500) + 1) * 500 as roundUp

It works fine for integer value:

@roundUpValue = ((@intValue / 1000) + 1) * 1000
@roundDownValue = (@intValue / 1000) * 1000

For example

declare @intValue as int = 1934
select ((@intValue / 1000) + 1) * 1000 as roundUp
select (@intValue / 1000) * 1000 as roundDown

If you want to round up to the nearest 500 then

select ((@intValue / 500) + 1) * 500 as roundUp
甜心小果奶 2024-09-15 12:13:29

没有原生函数可以做到这一点,但有许多简单的数学技巧可以做到这一点。一个例子:

DECLARE @Foo int
SET @Foo = 720

print @Foo
print (@Foo + 100) % 100
PRINT @Foo - (@Foo + 100) % 100

There's no native function that will do this, but there are any number of simple math tricks that will. An example:

DECLARE @Foo int
SET @Foo = 720

print @Foo
print (@Foo + 100) % 100
PRINT @Foo - (@Foo + 100) % 100
本王不退位尔等都是臣 2024-09-15 12:13:29

您可以使用此代码,假设您的 amount 是一个 int。如果不是,您将需要进行强制转换,以便获得整数除法。

If amount % 100 != 0 Then
   roundedAmount = ((amount / 100) * 100) + 100
Else
   roundedAmount = amount

您可能希望将其打包到用户定义函数

You can use this code, assuming your amount is an int. If not you will need to cast, so you get integer division.

If amount % 100 != 0 Then
   roundedAmount = ((amount / 100) * 100) + 100
Else
   roundedAmount = amount

You might want to package this into a user defined function.

玩套路吗 2024-09-15 12:13:29

通用解决方案 - 使用 MOD 找到最后 100 位,然后将结果加 100。

select (720 - MOD(720,100)) + 100 from dual;

如果您需要下一个第 80 位,只需将任何“100”替换为“80”即可。

A generic solution - Use MOD to find the last 100th place and then add 100 to the result.

select (720 - MOD(720,100)) + 100 from dual;

If you need the next 80th place, just replace any "100" with "80".

洋洋洒洒 2024-09-15 12:13:29

除了Gray的答案之外,
我将使用以下内联函数:

CREATE FUNCTION dbo.udf_RoundNearest
(
@Number bigint,
@RoundNearest bigint,
@Direction int
)

RETURNS TABLE AS 
RETURN

SELECT CASE WHEN @RoundNearest>=@Number THEN @Number
       ELSE
        (
                (@Number + CASE 
                           WHEN @Direction = 0 --Round Down
                           THEN 0
                           ELSE CASE WHEN @Number % @RoundNearest = 0 THEN 0 ELSE @RoundNearest END
                           END) / @RoundNearest) * @RoundNearest

   END Number

参数定义:

  1. @Number - 您需要舍入的数字
  2. @RoundNearest 10th、100th、1000th 等
  3. @Direction 0->圆形 向下1-> 进行舍入

使用该函数

SELECT * FROM dbo.udf_RoundNearest (1965,100,1) --> 2000
SELECT * FROM dbo.udf_RoundNearest (1359,100,0) --> 1300
SELECT * FROM dbo.udf_RoundNearest (1999,10,0) --1990
SELECT * FROM dbo.udf_RoundNearest (80,100,0) --> 80 (if the @number parameter is less or equal the @RoundNearest parameter the result will be the @number itself

:它也可以用作应用它与表格
例如:

;with tmp (Value) as
  (select 1236 union all select 6584 union all select 9999)

    select t.*, fn.Number
    from tmp t
    cross apply dbo.udf_RoundNearest (Value,100,0) fn

    /*Result Set
    Value   Number
    1236    1200
    6584    6500
    9999    9900*/

In addition to Gray's answer,
I'd use the following inline function:

CREATE FUNCTION dbo.udf_RoundNearest
(
@Number bigint,
@RoundNearest bigint,
@Direction int
)

RETURNS TABLE AS 
RETURN

SELECT CASE WHEN @RoundNearest>=@Number THEN @Number
       ELSE
        (
                (@Number + CASE 
                           WHEN @Direction = 0 --Round Down
                           THEN 0
                           ELSE CASE WHEN @Number % @RoundNearest = 0 THEN 0 ELSE @RoundNearest END
                           END) / @RoundNearest) * @RoundNearest

   END Number

Parameter Definition:

  1. @Number - the number you need to round
  2. @RoundNearest 10th, 100th , 1000th etc
  3. @Direction 0-> round down, 1-> round up

using the function:

SELECT * FROM dbo.udf_RoundNearest (1965,100,1) --> 2000
SELECT * FROM dbo.udf_RoundNearest (1359,100,0) --> 1300
SELECT * FROM dbo.udf_RoundNearest (1999,10,0) --1990
SELECT * FROM dbo.udf_RoundNearest (80,100,0) --> 80 (if the @number parameter is less or equal the @RoundNearest parameter the result will be the @number itself

it can also be used as apply it versus a table
such as:

;with tmp (Value) as
  (select 1236 union all select 6584 union all select 9999)

    select t.*, fn.Number
    from tmp t
    cross apply dbo.udf_RoundNearest (Value,100,0) fn

    /*Result Set
    Value   Number
    1236    1200
    6584    6500
    9999    9900*/
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文