sql - Access 中的大小写表达式

标签 sql ms-access

可以在 Access 中使用 case 表达式吗?我试图确定 2 列的最大日期,但在以下代码中不断出现语法错误:

CASE 
  WHEN dbo_tbl_property.LASTSERVICEDATE > Contour_dates.[Last CP12 Date]
    THEN dbo_tbl_property.LASTSERVICEDATE 
  ELSE Contour_dates.[Last CP12 Date] 
END AS MaxDate

最佳答案

您可以改用IIF() 函数。

IIF(condition, valueiftrue, valueiffalse)
  • condition 是您要测试的值。

  • valueiftrue 是条件评估为 TRUE 时返回的值。

  • valueiffalse 是条件计算结果为 FALSE 时返回的值。

还有Switch当您有多个条件需要测试时,该函数更易于使用和理解:

Switch( expr-1, value-1 [, expr-2, value-2 ] … [, expr-n, value-n ] )

The Switch function argument list consists of pairs of expressions and values. The expressions are evaluated from left to right, and the value associated with the first expression to evaluate to True is returned. If the parts aren't properly paired, a run-time error occurs. For example, if expr-1 is True, Switch returns value-1. If expr-1 is False, but expr-2 is True, Switch returns value-2, and so on.

Switch returns a Null value if:

  • None of the expressions is True.

  • The first True expression has a corresponding value that is Null.

NOTE: Switch evaluates all of the expressions, even though it returns only one of them. For this reason, you should watch for undesirable side effects. For example, if the evaluation of any expression results in a division by zero error, an error occurs.

关于sql - Access 中的大小写表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/772461/

相关文章:

c# - 简单示例子查询 Linq

mysql - 防止在 sql 脚本中的任何给定行之后执行

SQL Server 数据优化顾问错误

ms-access - 能否仅关闭 Access 中的某些警告?

forms - 在 Access 中创建 'calendar matrix'

ms-access - 以独占模式打开Access数据库

sql - 将文章直接插入 MediaWiki 数据库

mysql - 为什么在MySQL 5 中运行脚本时start transaction 和rollback 无法回滚?

sql - 检索具有字母数字格式的自动编号 ID

sql - 如果一列满足条件,如何选择一行,如果不满足,则选择另一列? [ Access 2010]