如果输出为负值,如何使其为空?

If output comes in negative value, how to make it null?

您好,我正在处理一个查询,我想做的是,每当输出为负值时, 我想将其设为 null,以下是我的查询,有人可以帮我解决这个问题吗?

select (to_char((coalesce(14515200/3600000,0) - (coalesce(30512.65/3600,0) - (coalesce(1800/3600,0) + (coalesce(1800/3600,0))))),'FM99,999,999,999'))::character varying as test

如果您希望负值的结果为 NULL,请使用

nullif(greatest(/* your expression */, 0), 0)

将您的逻辑移至 from 子句并使用 case:

select (case when test_n >= 0 then test_n::character varying end)
from (select . . .  as test_n -- your expression as number) x

如果您的表达式实际上使用了 table 中的值,您可以使用子查询、CTE 或横向连接来定义 test_n