The OR
in the WHEN
clause of a CASE
statement is not supported. How can I do this?
CASE ebv.db_no
WHEN 22978 OR 23218 OR 23219 THEN 'WECS 9500'
ELSE 'WECS 9520'
END as wecs_system
The OR
in the WHEN
clause of a CASE
statement is not supported. How can I do this?
CASE ebv.db_no
WHEN 22978 OR 23218 OR 23219 THEN 'WECS 9500'
ELSE 'WECS 9520'
END as wecs_system
You can use one of the expressions that WHEN has, but you cannot mix both of them.
WHEN when_expression
Is a simple expression to which input_expression is compared when the simple CASE format is used. when_expression is any valid expression. The data types of input_expression and each when_expression must be the same or must be an implicit conversion.
WHEN Boolean_expression
Is the Boolean expression evaluated when using the searched CASE format. Boolean_expression is any valid Boolean expression.
You could program:
1.
2.
But in any case you can expect that the variable ranking is going to be compared in a boolean expression.
See CASE (Transact-SQL) (MSDN).
That format requires you to use either:
Otherwise, use:
There are already a lot of answers with respect to
CASE
. I will explain when and how to useCASE
.You can use CASE expressions anywhere in the SQL queries. CASE expressions can be used within the SELECT statement, WHERE clauses, Order by clause, HAVING clauses, Insert, UPDATE and DELETE statements.
A CASE expression has the following two formats:
Simple CASE expression
This compares an expression to a set of simple expressions to find the result. This expression compares an expression to the expression in each WHEN clause for equivalency. If the expression within the WHEN clause is matched, the expression in the THEN clause will be returned.
This is where the OP's question is falling.
22978 OR 23218 OR 23219
will not get a value equal to the expression i.e. ebv.db_no. That's why it is giving an error. The data types of input_expression and each when_expression must be the same or must be an implicit conversion.Searched CASE expressions
This expression evaluates a set of boolean expressions to find the result. This expression allows comparison operators, and logical operators AND/OR with in each Boolean expression.
1.SELECT statement with CASE expressions
2.Update statement with CASE expression
3.ORDER BY clause with CASE expressions
4.Having Clause with CASE expression
Hope this use cases will help someone in future.
Source