Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it not possible to check list items in Case Statement's condition?

Tags:

case

oracle

I'm trying to check whether a list of items contain specific number in the condition part of a case statement in Oracle 10g. I get ORA-00936: missing expression error though. I'm trying to do something like the following:

Select case 'some_column_data' 
when in (1,2,3) then 'do_something' 
else 'do_something_else' end; 

So, is there any way to accomplish what I'm intended to do, or checking whether value is in list iteratively in different when statements is the only solution?

like image 253
Korhan Ozturk Avatar asked Mar 09 '12 13:03

Korhan Ozturk


People also ask

Can we use select query in CASE statement?

The case statement in SQL returns a value on a specified condition. We can use a Case statement in select queries along with Where, Order By, and Group By clause. It can be used in the Insert statement as well.

Can we use subquery in CASE statement?

A subquery in the ELSE clause works the same way as a subquery in the THEN clause. We use a passthru predicate to evaluate the subquery conditionally. Similarly, a CASE expression with multiple WHEN clauses with subqueries in each THEN clause also works the same way.

Can we use aggregate functions in CASE statement?

CASE statement in SQL and aggregate functions Aggregate functions in SQL Server perform calculations and return a single value. Examples of aggregate functions are MIN, MAX, COUNT, ABG and CHECKSUM. For this purpose, we use the COUNT aggregate function in SQL Server.

Can I have where clause in CASE statement?

Another way to use the Case Statement is within the WHERE clause. There, it may be utilized to alter the data fetched by a query based on a condition. Within that context, the Case Statement is ideally suited to both static queries, as well as dynamic ones, such as those that you would find inside a stored procedure.


1 Answers

How about using a "Searched CASE expression"

   CASE WHEN 'some_column_data' IN (1,2,3) 
        THEN 'do_something'
        ELSE 'do_something_else'
    END 

See here and here.

like image 98
Ollie Avatar answered Oct 26 '22 21:10

Ollie