What is the equivalent of Select Case in Access SQL?
Consider the Switch Function as an alternative to multiple IIf()
expressions. It will return the value from the first expression/value pair where the expression evaluates as True, and ignore any remaining pairs. The concept is similar to the SELECT ... CASE
approach you referenced but which is not available in Access SQL.
If you want to display a calculated field as commission
:
SELECT
Switch(
OpeningBalance < 5001, 20,
OpeningBalance < 10001, 30,
OpeningBalance < 20001, 40,
OpeningBalance >= 20001, 50
) AS commission
FROM YourTable;
If you want to store that calculated value to a field named commission
:
UPDATE YourTable
SET commission =
Switch(
OpeningBalance < 5001, 20,
OpeningBalance < 10001, 30,
OpeningBalance < 20001, 40,
OpeningBalance >= 20001, 50
);
Either way, see whether you find Switch()
easier to understand and manage. Multiple IIf()s
can become mind-boggling as the number of conditions grows.
MS Access Query with CASE statement
MS Access does not support CASE
statements. Use IIF
:
IIF(tsl.activitat <> '', tsl.activitat, ts.activitat ) AS Activitat
I'm not sure if Access supports aliases on LEFT JOIN
either, but it probably does
Note (although correctly tagged), the title of your question might be troublesome... most people refer to Transact-SQL (TSQL) when writing "MS SQL". Transact-SQL is used by MS SQL Server, but not by MS Access, which uses its own SQL dialect (called "Access SQL", and pretty limited in comparison)
Does MS Access support CASE WHEN clause if connect with ODBC?
You could use IIF
statement like in the next example:
SELECT
IIF(test_expression, value_if_true, value_if_false) AS FIELD_NAME
FROM
TABLE_NAME
Microsoft Access - Case Query
There is no CASE ... WHEN in Access SQL. You can use the Switch Function instead.
UPDATE HAI
SET REGION = Switch(
NUMREG Like '*1', 'BDG',
NUMREG Like '*2', 'JKT',
NUMREG Like '*3', 'KNG'
);
That query uses Access' default (ANSI 89 mode) * instead of % wildcard character. If you want to use the % wildcard, you can do it with the ALike comparison operator.
UPDATE HAI
SET REGION = Switch(
NUMREG ALike '%1', 'BDG',
NUMREG ALike '%2', 'JKT',
NUMREG ALike '%3', 'KNG'
);
Case expressions in Access
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.
CASE statement in Access 2013 (and how to use it)?
Thanks for Gord Thompson for giving me a clue, this is what I was looking for, it's working perfect :
expr1: Switch([date_]=Date(),"today",
[date_]=Date()-1,"yesterday",
[date_]>Date()-7,"week ago")
Related Topics
Set Limit for a Table Rows in SQL
How to Create a Cross Reference Table/Query for My Data
How to Give an Alias to a Table in Oracle
Understanding How Join Works When 3 or More Tables Are Involved. [Sql]
Optimizing Delete on SQL Server
SQL Function as Default Parameter Value
Execute Table-Valued Function on Multiple Rows
Sql: Sum 3 Columns When One Column Has a Null Value
Translating SQL Joins on Foreign Keys to R Data.Table Syntax
SQL Pulling a Row for Next or Previous Row of a Current Row
SQL Not Displaying Null Values on a Not Equals Query
Executing SQL Server Agent Job from a Stored Procedure and Returning Job Result
Order by with Inner Query, Giving Ora-00907 Missing Right Parenthesis