UPDATE with CASE and IN - Oracle UPDATE with CASE and IN - Oracle oracle oracle

UPDATE with CASE and IN - Oracle


You said that budgetpost is alphanumeric. That means it is looking for comparisons against strings. You should try enclosing your parameters in single quotes (and you are missing the final THEN in the Case expression).

UPDATE tab1   SET budgpost_gr1=   CASE                          WHEN (budgpost in ('1001','1012','50055'))  THEN 'BP_GR_A'                           WHEN (budgpost in ('5','10','98','0'))  THEN 'BP_GR_B'                          WHEN (budgpost in ('11','876','7976','67465')) THEN 'What?'                        ELSE 'Missing'                         END 


"The list are variables/paramaters that is pre-defined as comma separated lists". Do you mean that your query is actually

UPDATE tab1   SET budgpost_gr1=     CASE  WHEN (budgpost in ('1001,1012,50055'))  THEN 'BP_GR_A'         WHEN (budgpost in ('5,10,98,0'))  THEN 'BP_GR_B'        WHEN (budgpost in ('11,876,7976,67465'))           ELSE 'Missing' END`

If so, you need a function to take a string and parse it into a list of numbers.

create type tab_num is table of number;create or replace function f_str_to_nums (i_str in varchar2) return tab_num is  v_tab_num tab_num := tab_num();  v_start   number := 1;  v_end     number;  v_delim   VARCHAR2(1) := ',';  v_cnt     number(1) := 1;begin  v_end := instr(i_str||v_delim,v_delim,1, v_start);  WHILE v_end > 0 LOOP    v_cnt := v_cnt + 1;    v_tab_num.extend;    v_tab_num(v_tab_num.count) :=                   substr(i_str,v_start,v_end-v_start);    v_start := v_end + 1;    v_end := instr(i_str||v_delim,v_delim,v_start);  END LOOP;  RETURN v_tab_num;end;/

Then you can use the function like so:

select column_id,    case when column_id in      (select column_value from table(f_str_to_nums('1,2,3,4'))) then 'red'    else 'blue' endfrom  user_tab_columnswhere table_name = 'EMP'


Use to_number to convert budgpost to a number:

when to_number(budgpost,99999) in (1001,1012,50055) THEN 'BP_GR_A' 

EDIT: Make sure there are enough 9's in to_number to match to largest budget post.

If there are non-numeric budget posts, you could filter them out with a where clause at then end of the query:

where regexp_like(budgpost, '^-?[[:digit:],.]+$')