SQL函数用于处理三个参数的IFNULL是什么?

huangapple go评论76阅读模式
英文:

SQL function for IFNULL with three params?

问题

我知道 SQL 函数可以在项目为空时更改其值:

IFNULL(val, "was null")

是否有一种方法可以在项目为空时更改其值,并且在项目不为空时也更改它?换句话说,是否可以使用以下简写方式:

IF(val IS NULL, "was null", "was not null")
-->
IFNULL(val, "was null", "was not null")
英文:

I am aware of the sql function that will change the value of an item if that item is null:

IFNULL(val, "was null")

Is there a way to change the value if it is null, and also change it if it is not null? In other words, it would be a shorthand for:

IF(val IS NULL, "was null", "was not null")
-->
IFNULL(val, "was null", "was not null")

答案1

得分: 2

以下是您要翻译的内容:

通用的 ANSI SQL 方法是简单地使用 CASE 表达式:

CASE WHEN val IS NULL THEN 'was null' ELSE 'was not null' END AS label

在某些数据库中,比如 MySQL,有一个正式的 IF() 函数,它的工作方式与您在问题中描述的一样:

-- 来自 MySQL
IF(val IS NULL, 'was null', 'was not null') AS label
英文:

The generic ANSI SQL way of doing this would be to simply use a CASE expression:

<!-- language: sql -->

CASE WHEN val IS NULL THEN &#39;was null&#39; ELSE &#39;was not null&#39; END AS label

On some databases, such as MySQL, there is a formal IF() function which works exactly as you described in your question:

<!-- language: sql -->

-- from MySQL
IF(val IS NULL, &#39;was null&#39;, &#39;was not null&#39;) AS label

答案2

得分: 1

在MSSQL Server中,您可以使用IIF函数

 SELECT IIF(val is null, '是', '否'); 
英文:

In MSSQL Server you could use the IIF Function

 SELECT IIF(val is null, &#39;YES&#39;, &#39;NO&#39;); 

huangapple
  • 本文由 发表于 2023年6月6日 15:12:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76412215.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定