Access 中的大小写表达式 [英] Case expressions in Access

查看:26
本文介绍了Access 中的大小写表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

案例当 dbo_tbl_property.LASTSERVICEDATE >Contour_dates.[上次 CP12 日期]然后 dbo_tbl_property.LASTSERVICEDATEELSE Con​​tour_dates.[最后 CP12 日期]结束为最大日期

解决方案

您可以改用 IIF() 函数.

IIF(条件,valueiftrue,valueiffalse)

  • condition 是您要测试的值.

  • valueiftrue 是条件计算结果为 TRUE 时返回的值.

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

还有 切换函数,当你有多个条件测试时更容易使用和理解:

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

<块引用>

Switch 函数参数列表由成对的表达式和值.表达式从左到右计算,并且与要计算为 True 的第一个表达式关联的值是回.如果零件未正确配对,则会出现运行时错误发生.例如,如果 expr-1 为 True,则 Switch 返回 value-1.如果expr-1 为 False,但 expr-2 为 True,Switch 返回 value-2,依此类推

Switch 在以下情况下返回 Null 值:

  • 没有一个表达式为真.

  • 第一个 True 表达式的对应值为 Null.

注意:Switch 计算所有表达式,即使它只返回其中一个.出于这个原因,你应该注意不良的副作用.例如,如果评估任何表达式导致除以零错误,发生错误.

Can you use case expressions in Access? I'm trying to determine the max date form 2 columns but keep getting syntax errors in the following code:

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

解决方案

You can use the IIF() function instead.

IIF(condition, valueiftrue, valueiffalse)

  • condition is the value that you want to test.

  • valueiftrue is the value that is returned if condition evaluates to TRUE.

  • valueiffalse is the value that is returned if condition evaluates to FALSE.

There is also the Switch function which is easier to use and understand when you have multiple conditions to test:

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.

这篇关于Access 中的大小写表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆