Replace string while updating sql - 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

Related

inserting multiples values in one column

I have a question about SQL. I have created a table in SQL with only one column containing the name of two people (say John and Matt). Later I added a new column into the table with ALTER TABLE. This column will contain the surname of these people.
My question is, in case mmy table contained several people already is there a command to enter the surnames for all the people at once rather than writing one command for each person as in:
INSERT INTO table (Surname) VALUE (John's surname) and
INSERT INTO table (Surname) VALUE (Matt's surname) ?
Thanks in advance
P.D.
I tried something like:
UPDATE foo set Surname=("Parker","Walker") where Name =("John","Matt") but does not work
You want an update. Something like this:
update t
set surname = 'John'' surname'
where firstname = 'John';
You can do this separately for each name. Or use a case expression for multiple ones:
UPDATE foo
SET Surname = (CASE WHEN Name = 'John' THEN 'Parker'
WHEN Name = 'Matt' THEN 'Walker'
END)
WHERE Name IN ('John', 'Matt');

Updating with a select one table

I want to update a column with a concatenation of other columns within the same table.
This is for customer names in a table. There are separate columns for "Title" i.e. Mr, Ms, Mrs etc, "FirstName", "MiddleName" and "LastName". I altered the table by adding a new "FullName" column, which I tried to fill with a concatenation of the former columns.
SET [SalesLT].[Customer].[FullName] = (SELECT
Title,
FirstName,
MiddleName,
LastName,
CONCAT(Title,' ',FirstName,' ',MiddleName,' ',LastName) as FullName
FROM [AdventureWorksLT2008R2].[SalesLT].[Customer])
WHERE FullName = NULL;
I'm getting this
Only one expression can be specified in the select list when the subquery is not introduced with EXISTS.
Since you are updating a column value based on other columns in the same table, you not select them again.
UPDATE [SalesLT].[Customer]
SET [SalesLT].[Customer].[FullName] =
CONCAT(Title,' ',FirstName,' ',MiddleName,' ',LastName)
WHERE FullName is NULL;
Note: I did not change your where clause condition, neither concat function. In some databases you may need to do FullName is NULL instead of FullName = NULL. And in some databases, you may need to concat multiple values with ||.

Handling duplicates while updating records in SQL using where clause

I have a situation where i need to update a row in table and when faced with a duplicate entry then take a decision based on another column value.
For example let's say my table is like this : salary_table
Salary Username Usersurname Last_entry_date
3000 abc bak 20-feb-13
4000 sdf kup 20-mar-15
5000 abc bak 20-mar-15
so my update query is something like this
update salary_table
set salary=9000
where username=abc
and usersurname=bak;
For records like row 2 when there is unique entry this will not cause any problem
but i will get multiple rows for records like 1 (1 and 3) but i only want to update one row. In this case i would like to check last_entry_date. And the entry which has latest date (row 3 in this case) should get updated.
How can it be done ?
Any help would be highly appreciated.
Update salary_table
set salary = 9000
where username= 'abc'
and usersurname= 'bak'
and Last_entry_date = (select max(Last_entry_date)
from SalaryTable
where s.username = username
and s.usersurname = usersurname);
you have to add "where clause" on what you want in this case
"last_entry_date = ??"
With out adding proper filter how you identify which row to be updated.

Update statement is only inserting one value

I have data in one table (OWNER) i'm trying to use to update the data in another table (TAX_BILL_INFO). I've written an update statement to take the value from one column (owner.ownername) and insert that value into another column (tax_bill_info.mailername) where they have the same ID (taxbillid). below is my statement that I used:
UPDATE TAX_BILL_INFO
SET Mailername = OWNER.Ownername
FROM TAX_BILL_INFO INNER JOIN
OWNER ON TAX_BILL_INFO.TaxBillID = OWNER.TaxBillID
where taxyear = '2013' and (mailername = '' or mailername = ' ' or mailername is null) and (purchasername = '' or purchasername = ' ' or purchasername is null)
The columns are updating, it is only putting the FIRST value from OWNER into EVERY column in tax_bill_info. (ex. john smith, john smith, john smith). Are my where criteria throwing this off? Or could it be something else?
EDIT
If I use this select query:
select owner.OwnerName
FROM owner INNER JOIN
TAX_BILL_INFO ON TAX_BILL_INFO.TaxBillID = OWNER.TaxBillID
the names pull as they should. If i do a select query from the table I need the data entered into, it gives me the same dupliacte name for every row:
SELECT MAILERNAME
FROM TAX_BILL_INFO INNER JOIN
OWNER ON TAX_BILL_INFO.TaxBillID

update column only when it is null else dont update

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