gpt4 book ai didi

java - 为什么 Oracle 的 DECODE 给我的值与 NVL 不同?

转载 作者:搜寻专家 更新时间:2023-11-01 01:26:52 26 4
gpt4 key购买 nike

这个查询:

select nvl(0.75,0) from dual

给我0.75(数字)但是这个查询:

select decode(1,0,null,0.75) from dual 

给我 '.75'(字符串)。

为什么?

我试图通过将第二个查询更改为来解决此问题:

select decode(1,0,null,to_char(0.75,'0.99')) from dual

但在我的实际代码中,0.75 将是一个字段 (NUMBER),它可能具有不同的小数位数,我不应该从该值中添加/删除任何内容。

关于如何解决丢失的零问题但仍然支持所有可能的小数长度有什么想法吗?

最佳答案

是因为你的decode语句的第3个rd参数为NULL;根据 the documentation 1(我强调)。

Oracle automatically converts expr and each search value to the data type of the first search value before comparing.... If the first result has the data type CHAR or if the first result is null, then Oracle converts the return value to the data type VARCHAR2.

在您的例子中,第一个结果是 NULL,Oracle 将其视为 VARCHAR2。您的返回值被隐式转换为 VARCHAR2。如果您将 DECODE() 更改为以下内容,您会得到一个数字:

select decode(1, 0, 0, 0.75)

并且您可以通过使用 NULLIF() 来实现您的 NULL功能:

select nullif(decode(1, 0, 0, 0.75), 0) ...

最好使用 CASE 语句,它强制所有返回的数据类型相同:

select case 1 when 0 then null
else 0.75
end ...

<子>1。我也被抓到了。

关于java - 为什么 Oracle 的 DECODE 给我的值与 NVL 不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18749941/

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