Case expressions in Access
Asked Answered
P

3

36

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
Paisley answered 21/4, 2009 at 13:0 Comment(0)
T
56

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.

Tanhya answered 21/4, 2009 at 13:3 Comment(2)
i thought you had to have a true flase answer to do that?Paisley
"dbo_tbl_property.LASTSERVICEDATE > Contour_dates.[Last CP12 Date]" is your true/falseTension
S
9

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

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

Or for case function implementation example in VBA:

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

Regards, J.

Subdivision answered 21/4, 2009 at 13:36 Comment(1)
The switch function is a much better function to use if there are more multiple conditions being evaluated. It will be easier to read/understand/maintain compared to using nested IIF functions. Also, an alternate way to implement a default value instead of creating a logical expressions that basically evaluates to "all the previous expressions are false", is to just use true as the final expression to be evaluated. For example switch(dbo_tbl_property.LASTSERVICEDATE > Contour_dates.[Last CP12 Date], dbo_tbl_property.LASTSERVICEDATE, true ,Contour_dates.[Last CP12 Date])Coff
M
0

As long as you are dealing with numbers, CASE is just syntactic sugar. You can replace it with some boolean multiplication.

For example, CASE WHEN A = 1 THEN 2 ELSE 3 END is just a nicer way of writing

(-1) * ((A = 1) * 2 + (A <> 1) * 3)

(The -1 is needed because Microsoft Jet evaluates true boolean statements to -1.)

So instead of:

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

Use:

-1 * (
(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]
) AS MaxDate

By the way, sadly, if your CASE statement involves strings, this method does not work.

Myrwyn answered 8/9, 2020 at 19:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.