views:

358

answers:

3

Is there an equivalent for Oracle's decode() in Access (or Jet, for that matter).

The problem I am facing is: I should sort (order) a resultset based basically upon a status and a date (with all records having status = 2) at the end.

In Oracle I'd go something like

select
  ...
from
  ...
where
  ..
order by
  decode(status, 2, 0, 1),
  date_column
+1  A: 

You can try with IIF. See this stackoverflow question.

Pablo Santa Cruz
A: 

I think it might compare to switch or choose.

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

-- http://office.microsoft.com/en-us/access/HA012289181033.aspx

Choose(index, choice-1[, choice-2, ... [, choice-n]])

-- http://msdn.microsoft.com/en-us/library/aa262690%28VS.60%29.aspx

Remou
A: 

The closest analogy is the SWITCH() function e.g.

Oracle:

SELECT supplier_name,
       decode(supplier_id,  10000, 'IBM',
                            10001, 'Microsoft',
                            10002, 'Hewlett Packard',
                                   'Gateway') result
  FROM suppliers;

Access Database Engine

SELECT supplier_name,
       SWITCH(supplier_id = 10000, 'IBM',
              supplier_id = 10001, 'Microsoft',
              supplier_id = 10002, 'Hewlett Packard',
              TRUE, 'Gateway') AS result
  FROM suppliers;

Note that with the SWITCH() function you have to supply the full predicate each time, so you are not restricted to using just supplier_id. For the default value, use a predicate that is obvious to the human reader that it is TRUE e.g. 1 = 1 or indeed simply TRUE :)

Something that may not be obvious is that the logic in the SWITCH() function doesn't short circuit, meaning that every expression in the function must be able to be evaluated without error. If you require logic to short circuit then you will need to use nested IIF() functions.

onedaywhen