There are multiple column in my table and I want to skip one column if value is null for that one particular column in oracle update query? - sql

There are multiple column in my table and I want to skip one column if value is null for that one particular column and all other columns should be updated with there respective value in oracle update query.
Is there any simple way to do this ?
Example:
Let me explain my problem with example:
I am using merge query like below :
merge into table_X on (condition )
WHEN MATCHED THEN
UPDATE SET COLUMN_1=VALUE1 , COLUMN_2=VALUE_2,........
WHEN NOT MATCHED THEN
INSERT ( COLUMN_1,COLUMN_2...) VALUES (VALUE_1,VALUE_2);
Now in update statement I want to skip the update for one column suppose COLUMN_2 if its value is null , but all other column should be updated . Basically I want to preserve the existing value when null is coming .

Related

SQL: Update row based on a value

I have a table like:
|code|price|alter_price|
|----|-----|---------|
|ABC|10|12|
|DEF|11|13|
|GHI|15|18|
How can I update column 'price' and 'alter_price' with deviating values, based on the code-column (which has also the unique constrain)?
I don't want to create duplicates.
i tried:
cursor.execute("INSERT IGNORE INTO table_name VALUES (?,?,?)", (new_code,new_price,new_alter_price))
But then it does not update the table if the row with the given code exists already.

Insert null in one column on two columns same value in Oracle sql

i have inserted value from one table to another and i want to check if two columns values are same in a row insert null in one column of another table(table in which i am inserting values. In my example it is table1).
My existing query is given as follows how should i convert it according to my requirement.
INSERT INTO table1 (date1,date2)
SELECT substr(numtodsinterval(MAX(date1)-MIN(date2),'day'),
12,8)
FROM table2 where ....;

SQL Server : how to delete specific rows data with where condition in column?

I have a table with some columns and rows. I want to delete some rows contain specific data at specific row.
Ex: the table name is EXAM
I want to delete row 1 and row 3 when input condition string is C.
I did with the statement:
DELETE FROM EXAM
WHERE Comumn2 = 'C'
but only deleted Row 3.
To match rows which contain a specific value, use LIKE instead of =:
DELETE FROM EXAM WHERE Column2 LIKE '%C%'
Another Way of doing it by using CharIndex
DELETE FROM EXAM WHERE CHARINDEX('C',Column2)>0

Get values based on newly inserted value using SQL

I want to make filtration on a column after selecting a specific value of another column in the same table, I tried to use #... special character followed by the column's name to get the address of this value.
My SQL statement is like the following :
SELECT ATTRIBUTE FROM TABLE WHERE FIELD = '#FIELDNAME';
If I used a specific value instead of #FIELDNAME, it will work properly but it will be static but I need it to be dynamic based on the selected value.
Create another table which will have the list of values that are in the FIELDNAME and give each record a unique id ,then retrieve the value depending on what you have selected by the name of the new table's field preceded by '#...'
I don't know if that what are you looking for, please let me know.
If no triggers are allowed, do you have any date/time column in the table? Is it possible to have that extra column anyway to see the time of a newly inserted row?
You may have to check the lastest row entered, save its field value into a variable. Then do the select based on the variable value.
Based on the vague last row id you could try the following (it's not pretty). But again, if you have date/time that's more accurate.
select attribute from table
where field = (select field from table
where rowid =(select max(rowid) from table))
;
upate
Do you have the priviledge to set up your insert command as below:
insert into table (id, col1, col2,...) values (1,'something', 'something',...)
returning id into variable; -- you may either save field or id depending on your table
Then you may use this variable to select the records you want.

How to merge existing row with new data in SQLite?

I have a database full of simple note data, with columns for title, due date, priority, and details. There is also a _id column PRIMARY KEY int.
Say I have a note in the table already with some data filled and the rest null. I also have a set of data that will fill all those fields.
Is there a way that I can only write data to the fields that are NULL?
I can't overwrite existing data, but I'd like to add data to NULL columns.
I know the rowId of the target row.
If my target row had rowId of 5, I could do something like this:
UPDATE SET duedate='some date', priority='2', details='some text' WHERE _id=5
But that would overwrite all the data in that row, and I don't want to lose any data that might be there. How can I change this statement to avoid writing to non-null fields?
Suppose you start with
CREATE TABLE "t" ("a" , "b" , "c" );
INSERT INTO "t" ("a", "c") VALUES (1, 3);
Then
update t set a = coalesce(a,9), b = coalesce(b,10), c = coalesce(c,11);
Will update only the null values, ie. only column B will be set to 10. A and C will be left alone because they contain values.
Coalesce means pick the first item in the list that is not null.
The UPDATE statement only changes the fields you specify in the SET clause. If there are fields whose value you want left unmodified, then simply don't specify those fields in the SET clause.
Put another way, UPDATE doesn't write to all fields in the table - just the fields you specify for the rows you select with the WHERE clause.
If you simply don't know if the existing data is NULL or not, you can set the values using IFNULL(CurrentValue, NewValueIfNull). E.g.
UPDATE SET due_date=IFNULL(due_date, "some date") ... etc..
This will merge your new values into the row where there NULL values, and leave non-NULL values as they were.
See SQL Lite, IFNULL
How about:
UPDATE SET duedate='some date' WHERE _id=5 and duedate is null;
UPDATE SET priority='2' WHERE _id=5 and priority is null;
UPDATE SET details='some text' WHERE _id=5 and priority is null;
If you use Mysql, you can lookup IF()-then you can create a one liner. I think you can do something with similar in Oracle with case...