英文:
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 'was null' ELSE 'was not null' 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, 'was null', 'was not null') 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, 'YES', 'NO');
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论