SQL nvl 等效 - 没有 if/case 语句和 isnull 和合并

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/451060/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 00:46:36  来源:igfitidea点击:

SQL nvl equivalent - without if/case statements & isnull & coalesce

sqlsyntaxinformixnull

提问by CheeseConQueso

Are there any nvl() equivalent functions in SQL?

SQL 中是否有任何 nvl() 等效函数?

Or something close enough to be used in the same way in certain scenarios?

或者在某些情况下足够接近以相同方式使用的东西?



更新:


没有 if 语句


没有 case 语句


没有 isnull


没有合并




select nvl (purge_date,"SODIUFOSDIUFSDOIFUDSF") from id_rec where id=36581;


(expression)

SODIUFOSDIUFSDOIFUDSF

1 row(s) retrieved.

select isnull (purge_date,"SODIUFOSDIUFSDOIFUDSF") from id_rec where id=36581;

  674: Routine (isnull) can not be resolved.
Error in line 1
Near character position 8

select coalesce (purge_date,"SODIUFOSDIUFSDOIFUDSF") from id_rec where id=36581;

  674: Routine (coalesce) can not be resolved.
Error in line 1
Near character position 8

select decode(purge_date, NULL, "01/01/2009", purge_date) from id_rec where id=74115;

  800: Corresponding types must be compatible in CASE expression.
Error in line 1
Near character position 57

回答by BradC

ISNULL(for a single replace)

ISNULL(用于单个替换)

or

或者

COALESCE(Returns the first nonnull expression among its arguments.)

COALESCE(返回其参数中的第一个非空表达式。)

回答by RET

The problem with your DECODE statement that is generating the 800 error is simple. '01/01/2009'is being treated as a string, and it's actually the 4th argument that generates the error.

生成 800 错误的 DECODE 语句的问题很简单。'01/01/2009'被视为字符串,它实际上是产生错误的第四个参数。

Appreciate that the input and output of a DECODE statement can be different data-types, so the engine requires you to be more explicit in this case. (Do you want purge_datecast as a string or the string '01/01/2009', or the string argument parsed as a date or the original date? There's no way for the engine to know.

请注意 DECODE 语句的输入和输出可以是不同的数据类型,因此引擎要求您在这种情况下更加明确。(你想转换purge_date为 string 还是 string '01/01/2009',或者将 string 参数解析为日期或原始日期?引擎无法知道。

Try this:

尝试这个:

SELECT DECODE(purge_date, NULL, '01/01/2009'::DATE, purge_date)

You could also write that 3rd argument as:

您也可以将第三个参数写为:

    DATE('01/01/2009')
    MDY(1,1,2009)

depending on version and personal preference.

看版本和个人喜好。

回答by Quassnoi

You seem to be using Informix.

您似乎在使用 Informix。

AFAIK, there is DECODE there:

AFAIK,那里有解码:

DECODE(field, NULL, 'it is null, man', field)should give you same result as NVL(field, 'it is null, man')

DECODE(field, NULL, 'it is null, man', field)应该给你相同的结果 NVL(field, 'it is null, man')

Please post exact name and version of the RDBMS you are using.

请发布您正在使用的 RDBMS 的确切名称和版本。