Update with Case or If condition - sql

I am using PL/sql on APEX.
I have a simple table, where one of the column I need to update the field using Case or if condition.
Just can't this right.
e.g.
Update TableAA
set column1 =
Select case
when SUBSTR(column2,-5,5) ='xxx11' then 'xx1xx'
when SUBSTR(column2,-4,4) = 'y1y1' then 'yyyy'
else '9999999' end as column1
from TableAA;

Since you are updating records from same table, why not try:
Update TableAA set column1 =
case
when SUBSTR(column2,-5,5) ='xxx11' then 'xx1xx'
when SUBSTR(column2,-4,4) = 'y1y1' then 'yyyy'
else '9999999' end;

Related

How can I update various sql columns based on another column of the same row?

I am running SQL Server and am needing to update hundreds of entries. Column A has unique values and based on that I need to update column B of the same table, all with different values. Essentially I am looking for a way to perform the following but in a bulk manner:
UPDATE table
set column B = 'value'
where column A = 'unique value'
Is this what you are expecting,
UPDATE table
set column B = case when column A = 5 then 'unique string' end;
How about:
update table set
columnB = concat('unique ', columnA)
you may, or may not, need a where clause, depending on your needs.
With hundreds of values you better have the {unique value - update value} mapping defined in a separate table as TT is asking. But otherwise, if you are okay with hard-coding those mappings in your UPDATE statement:
UPDATE t
SET B = CASE A
WHEN 'unique value1' THEN 'value1'
WHEN 'unique value2' THEN 'value2'
WHEN 'unique value3' THEN 'value3'
ELSE 'Unknown'
END
FROM table t
BTW, the above is only for 3 mappings
Here, you need to write cursor to update a column value in the same table
BEGIN
DECLARE c_a, c_b TEXT;
DECLARE c_ab CURSOR FOR
SELECT column_a,column_b FROM c_table;
OPEN c_ab;
LOOP
FETCH c_ab into c_ca, c_cb;
IF c_ca = "0" THEN
update c_table SET column_b = "No" WHERE
column_a = c_ca;
END IF;
IF c_ca = "1" THEN
update c_table SET column_b = "Yes" WHERE
column_a = c_ca;
END IF;
END LOOP;
CLOSE c_ab;
END
Working and tested code [please refer some cursor tutorials to update according to your condition] and with this you can update table in bulk and speedy
Thanks #Jim Macaulay. That did it. Thank you everyone else for your input.

Update query if statement for Oracle

I need to update newly created column in my oracle table. To do so I need to use existing values in row to decide how to populate this column, I am getting error:
java.lang.NullPointerException -> See Debug Output for details
This is my query:
UPDATE
SCHEMA_NAME.TABLE_NAME
SET
OCO= IF CO= 'Y' AND COM='Y' THEN
{
'Y'
} ELSE
{
'N'
}
END IF;
Any suggestions on syntax?
You could use CASE expression in the SET clause.
For example,
UPDATE table
SET schema.column = CASE
WHEN CO= 'Y' AND COM='Y' THEN
'Y'
ELSE
'N'
END

INSERT based on another columns value

I want to update a column 'XYZ_RCVD' to 'Y' if there is a value in the row, if not i want to update column 'XYZ_RCVD' to 'N'. I started with this simple script that worked but I had to modify it for the 'N' case. How would i merge both of them into one? I tried some IF-THEN-else but that didn't work too well with the INSERT statement.
Here is what i have so far.
UPDATE TEST_SURVEY
SET XYZ_RCVD = 'Y'
WHERE XYZ_NAME IS NOT NULL;
UPDATE TEST_SURVEY
SET XYZ_RCVD = 'N'
WHERE XYZ_NAME IS NULL;
The solution below by Habib worked perfectly.
I believe you are looking for CASE WHEN like:
UPDATE TEST_SURVEY
SET XYZ_RCVD = CASE WHEN XYZ_NAME IS NOT NULL THEN 'Y' ELSE 'N' END;

query: if condition in sql server update

I have a SQL server table in which there are 2 columns that I want to update either of their values according to a flag sent to the stored procedure along with the new value, something like:
UPDATE
table_Name
SET
CASE
WHEN #flag = '1' THEN column_A += #new_value
WHEN #flag = '0' THEN column_B += #new_value
END AS Total
WHERE
ID = #ID
What is the correct SQL server code to do so??
I thought M.Ali's comment was correct, so I've constructed this based on his suggestion.
I'm also assuming the status field is 'approved' or 'declined' as you say based on if it's populated or not. If there are any other conditions on the status field, offcourse you must add these to you where statements
BEGIN TRANSACTION
Update Payment
set post_date = new_postdate_value
account_num = new_account_num_value
pay_am = new_pay_am_value
pay_type = new_pay_type_value
authoriz = new_authoriz_value
where status is not null
UPDATE Payment
SET account_num = new_account_num_value
WHERE status is null
COMMIT TRANSACTION

change with column that is updated based on is null

Hi I have an update query where if one field is null then I want value goes to another field
here is an example of what I would like to do:
UPDATE Table SET
CASE WHEN Column1 is NULL
THEN Column2 = #Update
ELSE Column1 = #Update
END;
Now this does not work as I get an error on the word case but is there a way to do what I am try to accomplish? I know it could be done if I used two update statements with a where but was wondering if it could be done in one query?
You are using incorrect syntax for CASE in SQL statements. CASE must return an expression, it cannot contain a statement in pure SQL.
update Table
Set Column1 = Case when Column1 is NULL Then NULL Else #Update END,
Column2 = Case when Column1 is NULL Then #Update Else Column2 END