Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you select all columns, plus the result of a CASE statement in oracle 11g?

I want to select *, and not have to type out all individual columns, but I also want to include a custom column with a case statement. I tried the following:

select *, (case when PRI_VAL = 1 then 'High'                 when PRI_VAL = 2 then 'Med'                 when PRI_VAL = 3 then 'Low'           end) as PRIORITY from MYTABLE; 

But it is complaining that

ORA-00923: FROM keyword not found where expected 
like image 562
Kevin Pauli Avatar asked Nov 17 '09 21:11

Kevin Pauli


People also ask

What does column (+) mean in Oracle?

The plus sign is Oracle syntax for an outer join. There isn't a minus operator for joins. An outer join means return all rows from one table. Also return the rows from the outer joined where there's a match on the join key. If there's no matching row, return null.

Can we use case in SELECT statement in Oracle?

Introduction to Oracle CASE expression You can use a CASE expression in any statement or clause that accepts a valid expression. For example, you can use the CASE expression in statements such as SELECT , UPDATE , or DELETE , and in clauses like SELECT , WHERE , HAVING , and ORDDER BY .

How do you write a SELECT query in a CASE statement?

insert into table table_name ( value1, value 2,value 3) select (value 1,value2 , case value3 when value1 = 'somevalue' &&* value2 = 'somevalue' then 'x' else 'y' End from table_name.

Can CASE statement return multiple values in Oracle?

A CASE statement cannot return more than one value, it is a function working on one value.


1 Answers

Add an alias for mytable like this:

select t.*, (case when PRI_VAL = 1 then 'High'                 when PRI_VAL = 2 then 'Med'                 when PRI_VAL = 3 then 'Low'           end) as PRIORITY from MYTABLE t; 

This is not dependent on any specific Oracle version, not sure about other databases.

like image 156
Thorsten Avatar answered Oct 24 '22 22:10

Thorsten