SQL 在 postgres 中解码等价物
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15262351/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Decode equivalent in postgres
提问by vmb
There is no equivalent to the Oracle
's DECODE()'Function In
Postgres`. Is there anyone who wrote decode as a Function?
没有等效于Oracle
' DECODE()'Function In
Postgres`。有没有人把解码写成一个函数?
采纳答案by Mitch Wheat
There is an equivalent. It's called a CASE
statement.
有一个等价物。这叫做CASE
声明。
There are two forms of CASE:
CASE有两种形式:
Simple CASE:
简单案例:
CASE search-expression
WHEN expression [, expression [ ... ]] THEN
statements
[ WHEN expression [, expression [ ... ]] THEN
statements
... ]
[ ELSE
statements ]
END CASE;
Searched CASE:
搜索案例:
CASE
WHEN boolean-expression THEN
statements
[ WHEN boolean-expression THEN
statements
... ]
[ ELSE
statements ]
END CASE;
CASE
statements are easier to read; I prefer these over decode()
in Oracle.
CASE
陈述更容易阅读;decode()
在 Oracle 中,我更喜欢这些。
回答by mvp
If you are used to Oracle specific functions, you might want to install PostgreSQL extension orafce
.
如果您习惯使用 Oracle 特定功能,则可能需要安装 PostgreSQL 扩展orafce
。
Among other Oracle specific functions, orafce
also implements DECODE
- one that you are looking for.
在其他 Oracle 特定功能中,orafce
还实现DECODE
了您正在寻找的功能。
If you are running on Ubuntu, you will simply need to install package postgresql-9.1-orafce
to make orafce
available in your PostgreSQL server.
如果您在 Ubuntu 上运行,您只需要安装软件包postgresql-9.1-orafce
即可orafce
在您的 PostgreSQL 服务器中使用。
回答by Fernando Meneses Gomes
You can combine NULLIF with COALESCE:
您可以将 NULLIF 与 COALESCE 结合使用:
SELECT COALESCE(NULLIF(value, 0), newValue) FROM table;
SELECT COALESCE(NULLIF(value, 0), newValue) FROM table;
Font: Coalesce for zero instead of null
字体:合并为零而不是空