Is there any way to change field value based on its current value with one query?
Like I have tbl.team and if it's value = 1, change it to 2. And vice versa, tbl.team = 2 => 1.
You can use a case expression to update the column conditionally:
update the_table
set team = case
when team = 1 then 2
else 1
end
where team in (1,2);
Related
I'm creating a new table and carrying over several columns from a previous table. One of the new fields that I need to create is a flag that will have values 0 or 1 and value needs to be determined based on 6 previous fields in the table.
The 6 previous columns have preexisting values of 0 or 1 stored for each one. This new field needs to check whether any of the 6 columns have 1 and if so set the flag to 0. If there is 0 in all 6 fields then set itself to 1.
Hopefully this makes sense. How can I get this done in oracle? I assume a case statement and some sort of forloop?
You can use greatest() function: GREATEST
create table t_new
as
select
case when greatest(c1,c2,c3,c4,c5,c6)=1 -- at least one of them contains 1
then 0
else 1
end c_new
from t_old;
Or even shorter:
create table t_new
as
select
1-greatest(c1,c2,c3,c4,c5,c6) as c_new
from t_old;
In case of greatest = 1, (1-1)=0, otherwise (1-0)=1
You can use a virtual column with a case expression; something like:
flag number generated always as (
case when val_1 + val_2 + val_3 + val_4 + val_5 + val_6 = 0 then 1 else 0 end
) virtual
db<>fiddle
or the same thing with greatest() as #Sayan suggested.
Using a virtual column means the flag will be right for newly-inserted rows, and if any of the other values are updated; you won't have to recalculate or update the flag column manually.
I've assumed the other six columns can't be null and are constrained to only be 0 or 1, as the question suggests. If they can be null you can add nvl() or coalesce() to each term in the calculation.
id value
1 a
2 b
3 c
How do i add second value 'z' to id=1 (separated by comma)?
id value
1 a,z
2 b
3 c
and how to remove the 'z' now if i have that final table?
You can use update:
update t
set value = concat(value, ',z')
where id = 1;
To answer your secondary question, yes.
If you run Select value from table where id = 1 it will return a,z. that means that if you are going to use it again in queries, you will quite possibly need to utilize a Split() type function, dependent on what you're doing with it.
The best and simplest way to do this is the following query according to me :
update table1 set value = concat(value,'z') where id = 1
where : Table1 is the name of your table.
Based on one column within my query results (Value), I am trying to write an if/else statement based on the value held which will display the result the in an additional row.
For example, if I have a record of 2 within the value field, but I want to check whether it is above < 5. If the value is less than 5 I basically want the additional column to display a hardcoded value of 5, else display actual value.
Any help would be appreciated.
Use a case statement
select a.*,
case
when a.TheField < 5 then 5
else a.TheField
end as NewField
from MyTable a
You can use a case
select value, case when value < 5
then 5
else value
end as calculated_column
from your_table
I have a table kyc3 where there are walletno, status and rank columns present. rank columns are currently filled with 0. While status column has following data: accepted, rejected, registered and scanned. I want to put value for each status in rank column where
accepted = 1, rejected = 2, registered = 3 and scanned = 4
I wrote following query but do not understand how to complete it:
INSERT INTO kyc3([rank]) SELECT status_ FROM kyc3
I understand I need to put a where clause that will indicate my logic for data population. But what should I write?
If the table is populated and you want to change the rank field, you want to use an UPDATE statement, as INSERT is for adding new rows to your table:
UPDATE kyc3
SET rank = CASE WHEN status = 'accepted' THEN 1
WHEN status = 'rejected' THEN 2
WHEN status = 'registered' THEN 3
WHEN status = 'scanned' THEN 4
END
You can use update to fill a cell of an existing row.
update kyc3
set rank = CASE WHEN status = 'accepted' THEN 1
WHEN status = 'rejected' THEN 2
WHEN status = 'registered' THEN 3
WHEN status = 'scanned' THEN 4
END
Use insert only for creating new rows.
I want to update table A in such a way that if the attribute of the table column is desired then only it will change otherwise it wont change..
Update table A set B="abcd" ,C= (case when C="abc" then C="abcd" else C end) where column =1;
means C should be only change when in column=1 and C value is abc otherwise C should not be update ..it should be dropped and only B changes. but if the C
value get matched i.e abc give me the output 0 .. not changing to the abcd
Inside the THEN part, C="abcd" compares C with the value, and returns either 1 or 0.
The entire CASE expression should just return a value that then gets written into the C column, so you want just 'abcd' in this place:
UPDATE tableA
SET B = 'abcd',
C = CASE
WHEN C = 'abc' THEN 'abcd'
ELSE C
END
WHERE column = 1;
If I understand you correctly, you're trying to do two separate things:
UPDATE A set B = 'abcd' WHERE column = 1
UPDATE A set C = 'abcd' WHERE C = 'abc' AND column = 1
Is that right? If so, can you do it as two simple statements instead of one complicated statement?