SQL Access 中的大小写表达式

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

Case expressions in Access

sqlms-access

提问by Mitch Wheat

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

您可以case在 Access 中使用表达式吗?我正在尝试确定 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

回答by Mitch Wheat

You can use the IIF()function instead.

您可以改用该IIF()功能。

IIF(condition, valueiftrue, valueiffalse)
  • conditionis the value that you want to test.

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

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

  • condition是您要测试的值。

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

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

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

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.

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

如果出现以下情况,Switch 将返回 Null 值:

  • 没有一个表达式为真。

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

注意: Switch 计算所有的表达式,即使它只返回其中之一。因此,您应该注意不良副作用。例如,如果对任何表达式的求值导致除以零错误,则会发生错误。

回答by juckobee

There is no case statement in Access. Instead you can use switch statement. It will look something like the one below:

Access 中没有 case 语句。相反,您可以使用 switch 语句。它看起来像下面这样:

switch(dbo_tbl_property.LASTSERVICEDATE > Contour_dates.[Last CP12 Date],dbo_tbl_property.LASTSERVICEDATE,dbo_tbl_property.LASTSERVICEDATE <= Contour_dates.[Last CP12 Date],Contour_dates.[Last CP12 Date])

switch(dbo_tbl_property.LASTSERVICEDATE > Contour_dates.[Last CP12 Date],dbo_tbl_property.LASTSERVICEDATE,dbo_tbl_property.LASTSERVICEDATE <= Contour_dates.[Last CP12 Date],Contour_dates.[Last CP12 Date])

For further reading look at: http://www.techonthenet.com/access/functions/advanced/switch.php

如需进一步阅读,请查看:http: //www.techonthenet.com/access/functions/advanced/switch.php

Or for case function implementation example in VBA:

或者对于 VBA 中的 case 函数实现示例:

http://ewbi.blogs.com/develops/2006/02/adding_case_to_.html

http://ewbi.blogs.com/develops/2006/02/adding_case_to_.html

Regards, J.

问候,J。