update column only when it is null else dont update - sql

Is there a way to update a column only when it is null and leave it as it is when it is not null in one multiple-columns-update query?
something like below. (like how we use case in select statements)
UPDATE users SET users.city = 'Dallas',
CASE
WHEN users.Global_id IS NULL
THEN
users.Global_id = '123'
END WHERE userid = '12312312'
The above update statement throws ORA-00927: missing equal sign error.
Reason why I am looking for this?
I have a schedule job that runs a similar query like above.
And there also exists a trigger in users table that will raise error if you try to update a 'not null' global_id. So my job fails when it encounters this trigger.
I have one option... to split this update query to two.. one to update city and one to update global_id where global_id is null.
But wondering if this can be achieved by any other way... Any idea would be appreciated.

There's a couple of options, but the closest one to your code is this (reformatted and with redundant content removed):
UPDATE users SET
city = 'Dallas',
Global_id =
CASE
WHEN Global_id IS NULL THEN '123'
ELSE Global_id
END
WHERE userid = '12312312';
However, this is equivalent to the more concise use of the coalesce() function:
...
Global_id = COALESCE(Global_id, '123')
...

You can use coalesce function:
UPDATE users
SET
users.city = 'Dallas',
users.Global_id = coalesce( users.Global_id, '123')
Also, you can split your update in two sentences, remember than you can enclose it in a single transaction:
UPDATE users
SET
users.city = 'Dallas';
UPDATE users
SET
users.Global_id = '123'
WHERE
users.Global_id is null

let say you have this column name is not updated city
(UPDATE users SET city = 'Dallas' WHERE Global_id = your_variable_of_user
AND city = "" ) // it means it null here.
the second sql of global id
(UPDATE users SET city = 'Dallas' WHERE Global_id = 0
AND city = "" ) // it means it null here.
user_id is your column name of the user id you use
do you mean global_id will be updated? because if global_id is 0 it means its guest its not logged in so how you will update it ?
but i advice you to use PDO .
hope it helps u

Related

Replace string while updating sql

I have table called Empdetails. Here one of column name is loginid. I want to replace loginid if it contain #s.com, in few case it has.
Loginid
abc#s.com
sdf
ghj
adfgh#j.com
fghjku#s.com
pinky#s.com
update Empdetails
set loginid = REPLACE(loginid, '#s.com', '')
where id in (1,6,8,9)
If I mistakenly mention id whose loginid does not contain any '#s.com', will throw an error. Is above query is fine
To answer your question, your query will not end in Error.
The below will only update records which end with '#s.com'
update Empdetails
set loginid = REPLACE(loginid, '#s.com', '')
where loginid like '%#s.com'
You don't need to specify ID, you can just use your logic to filter records which you want to update

How to update column based on filter of another column?

I'm trying to basically update a subset of rows in a table using a filter but currently my query is updating all values instead of the intended subset. I'm not really sure what I'm missing.
Here's what the current (non working) update looks like.
UPDATE account_type
SET type_id = 3
FROM my_filter;
I have been able to successfully select the data I want, I just can't figure out how to get the update to work. I am able to access the fields correctly with the following select statement.
SELECT account_type FROM my_filter;
But when I attempt to update, it updates every field. Here's what the (working) join I have looks like, wrapped in a CTE.
WITH my_filter AS
(
SELECT account_type.type_id, username
FROM account_type
INNER JOIN user ON user.account_id = account_type.account_id
WHERE username LIKE 'filter%'
)
You need a join condition. I would guess:
UPDATE account_type
SET type_id = 3
FROM my_filter
WHERE account_type.type_id = my_filter.type_id;
Alternative, you can write this as:
UPDATE account_type
SET type_id = 3
FROM user
WHERE user.account_id = account_type.account_id AND
user.username LIKE 'filter%';

Perform multiple updates in a single query in an access database table

In my access 2016 database, I have a table tblCustomerInfo with a field customer_code. There are some old customer_code values that need to be updated to newer values (for example, all rows with customer_code = 103 should be updated to customer_code = 122).
I can achieve something like this going one customer_code at a time using queries like:
UPDATE tblCustomerInfo set customer_code = 122 Where customer_code = 103;
UPDATE tblCustomerInfo set customer_code = 433 Where customer_code = 106;
...
however, I would like to avoid having to run a separate query for each customer_code. Is there any way to update all the codes, each to a different new value, in a single query?
Create a table eg CustomerCodes with two fields oldcode and newcode, and add in all the values. Then run an update query like this:
UPDATE CustomerCodes INNER JOIN tblCustomerInfo
ON CustomerCodes.OldCode = tblCustomerInfo.Customer_Code
SET Customers.Customer_Code = [CustomerCodes].[NewCode];
Alternative
If there aren't too many to change you can use a switch statement like this:
UPDATE tblCustomerInfo
SET Customer_Code =
SWITCH(Customer_Code=103,126,
Customer_Code = 106,130,
Customer_Code = 107,133);
There is, in my experience, a limit on the number of pairs you can have in a switch statement, although I have never bothered to find out exactly what the limit is - it doesn't seem to be documented.

How to delete a row in SQL based on a NULL condition

I just started learning SQL; I've created a table. Learned insert command and inserted values in 2 rows. However I've inserted null values in 3rd.
Now I want to delete the third row which has 2 columns with no values in it.
I'm using the following query:
delete employee where city=null;
It doesn't seem to be working!
According SQL 92 standard many logical operations with null values like
> null
= null
and null
or null
not null
should always return null (and never true). Some DBMS (e.g. Oracle) follow this rule rigorously, some (MS SQL) can have a mode that null = null returns true, not required null. In order to be compartible with SQL 92 and so with (almost) all DBMSs, you should use is null or is not null standard comparisons, in your case
delete from employee
where city is null -- <- Standard comparison
You need the is null "operator":
delete from employee where city is null;
This is because in SQL, nothing is equal to NULL.
You can't use = with NULL. Instead, use:
delete employee where city is null;
To achieve this
you will have to write this query
DELETE from table_name WHERE city IS NULL;
this query will delete the rows/records WHERE city is null
Here you know you have added Null in city column so checking with is null will work but it's also possible to add an empty string in city column. So if your business condition is like delete all records from employee table where city is either null or empty i.e. with no values you should write as:
delete from employee where isnull(city,'')='';
change it :
delete from table_name where city is null
or
delete from table_name where city = null

SQL Syntax - Edit a column record

I have a table called User. In the User table, there's a column called country. How do I write an SQL syntax to change all the user records whose country is "a" to country "b"?
This is a classic update statement:
UPDATE user
SET country = 'b' -- what to change
WHERE country = 'a' -- on which records
update "User" set "Country" = 'b' where "Country" = 'a'
PS: SQL is compatible with many backends. You didn't specify which one you have. That matters, because User maybe a keyword in some backends and/or casing might be significant.
UPDATE [User]
SET [country] = 'b' -- new value
WHERE [country]='a' -- previous value