update with case and in

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' end
from user_tab_columns
where table_name = 'EMP'
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
`CASE` is a conditional statement in MySQL that allows you to evaluate a series of conditions and return a value based on the first condition that is true. You can use `CASE` in conjunction with SQL statements like `SELECT`, `UPDATE`, and `DELETE`. Here is an example of using `CASE` in a `SELECT` statement: ``` SELECT order_id, CASE WHEN order_total >= 1000 THEN 'High' WHEN order_total >= 500 THEN 'Medium' ELSE 'Low' END AS order_priority FROM orders; ``` In this example, we are selecting the `order_id` column from the `orders` table and creating a new column called `order_priority`. The value of `order_priority` is determined by the `CASE` statement. If `order_total` is greater than or equal to 1000, then `order_priority` is set to 'High'. If `order_total` is greater than or equal to 500, then `order_priority` is set to 'Medium'. Otherwise, `order_priority` is set to 'Low'. You can also use `CASE` in conjunction with other SQL statements, such as `UPDATE`. Here is an example of updating a table using `CASE`: ``` UPDATE employees SET salary = CASE WHEN department = 'Sales' THEN salary * 1.1 WHEN department = 'Marketing' THEN salary * 1.2 ELSE salary END; ``` In this example, we are updating the `salary` column in the `employees` table. The new value of `salary` is determined by the `CASE` statement. If the `department` is 'Sales', then `salary` is multiplied by 1.1. If the `department` is 'Marketing', then `salary` is multiplied by 1.2. Otherwise, `salary` remains the same. Overall, `CASE` is a useful tool in MySQL for creating conditional statements and controlling the flow of your SQL queries.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值