gpt4 book ai didi

sql - Oracle SQL,仅舍入大于 'half'

转载 作者:行者123 更新时间:2023-12-03 03:19:23 27 4
gpt4 key购买 nike

我需要对数字进行“半舍入”并保留 (.5),例如:

如果数字 = (9115.7) 则 (9116)

如果数字 = (9115.5) 则 (9115.5)

如果数字 = (9115.3) 则 (9115)

最佳答案

您可以使用 case 表达式重写自己的舍入逻辑:

CREATE TABLE t
AS
SELECT 9115.7 AS col FROM dual UNION ALL
SELECT 9115.5 AS col FROM dual UNION ALL
SELECT 9115.3 AS col FROM dual;

SELECT col, CASE WHEN MOD(ABS(col),1) = 0.5 THEN col ELSE ROUND(col) END
FROM t;

<强> db<>fiddle demo

输出:

+---------+---------+
| COL | ROUNDED |
+---------+---------+
| 9115.7 | 9116 |
| 9115.5 | 9115.5 |
| 9115.3 | 9115 |
+---------+---------+

关于sql - Oracle SQL,仅舍入大于 'half',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57637069/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com