How to update a sql record text, by replacing a string for various text? - sql

I have to update a table record where I am replacing some old email ids with a new id.
I am at the moment doing replace like
replace(replace(replace( column, 'abd#xyz.com', 'new#email.com),'old#re.com', 'new#email.com'),'asda#sdfsd.f', 'new#email.com')
Is there any simple way to do it?

I am not sure why a column would contain an email and other information. So perhaps this does what you want:
update t
set column = 'new#email.com'
where column in ('abd#xyz.com', 'old#re.com', 'asda#sdfsd.f');
If the column contains additional information, I would recommend that you fix your data model rather than trying to make a broken data model work.

Related

how to change specific text in a column but keep the rest

I am using SQL in navicat. I would like to change the UID below to id and keep the rest of the text in each column. The numbers after the = are different in each record.
EXAMPLE:
3PYTW2X?UID=5493139 and I want it changed to 3PYTW2X?id=5493139
3PYTW2X?UID=2986225 and I want it changed to 3PYTW2X?id=2986225
I have about 200k records that need to be updated.
Thank you in advance!
Use a replace function:
REPLACE(FieldName,'UID','ID')

Database manipulation

Let us say that we have an SQL database with a contacts table that has 5 fields; contactID, title, firstName, middleName, and lastName. On the front end we have a profile page with an update button.
Let us also say that we want to allow the user to update any given field without having to also enter data in the other fields (a last name change, for example.). Is there a 'simple' way to allow this?
My solution is to add a bit field to each property and add a series of condition statements to generate the UPDATE statement based on the bit field.
It is ugly and seems inefficient. Any advice?
The best way is to keep data retrieved from database in all fields and update all the fields. If user changes the value, it will be updated to new value. Otherwise it will stay the same old value.
Suppose that you retrieved all the values from database and on click of edit button, they are editable(textboxes) with old value already present in them. Then write a query like
update contacts set title =#title, firstName=#firstName, middleName=#middleName, lastName=#lastName where contactID=#ContactID

How to deselect values in multivalue lookup field using a query

I want to delete all the data of a single field. This field is a ''multipled value lookup field'';
I tried this query in the SQL design tab:
UPDATE _myDatabase SET [_myDatabase].mycolumn= Null;
but it's not working (although it works for a any non-lookup fields). Any idea how to solve this?
Because you are using a multi value field, you must treat the field as a separate table, which it actually is under the hood.
So you use a DELETE statement:
DELETE _myDatabase.mycolumn.Value FROM _myDatabase

Appending data to a MySQL database field that already has data in it

I need to "add" data to a field that already contains data without erasing whats currently there. For example if the field contains HTML, I need to add additional HTML to the field. Is there a SQL call that will do this or do I need to call the data in that field, concatenate the new data to the existing data, and reload it into the database?
UPDATE Table SET Field=CONCAT(Field,'your extra html');
UPDATE myTable SET html=concat(html,'<b>More HTML</b>') WHERE id='10'
... for example. Your WHERE would be different of course.
Append at the end of a field, separated with with a line break:
UPDATE Table SET Comment = CONCAT_WS(CHAR(10 USING UTF8), Comment, 'my comment.');
CONCAT_WS() appends multiple strings separated by a given separator.
CHAR(10, UTF8) is a line break.
UPDATE Table SET Field=CONCAT(IFNULL(Field, ''), 'Your extra HTML')
If the field contains NULL value then CONCAT will also return NULL. Using IFNULL will help you to update column even it has NULL value.

query a table not in normal 3rd form

Hi I have a table which was designed by a lazy developer who did not created it in 3rd normal form. He saved the arrays in the table instead of using MM relation . And the application is running so I can not change the database schema.
I need to query the table like this:
SELECT * FROM myTable
WHERE usergroup = 20
where usergroup field contains data like this : 17,19,20 or it could be also only 20 or only 19.
I could search with like:
SELECT * FROM myTable
WHERE usergroup LIKE 20
but in this case it would match also for field which contain 200 e.g.
Anybody any idea?
thanx
Fix the bad database design.
A short-term fix is to add a related table for the correct structure. Add a trigger to parse the info in the old field to the related table on insert and update. Then write a script to [parse out existing data. Now you can porperly query but you haven't broken any of the old code. THen you can search for the old code and fix. Once you have done that then just change how code is inserted or udated inthe orginal table to add the new table and drop the old column.
Write a table-valued user-defined function (UDF in SQL Server, I am sure it will have a different name in other RDBMS) to parse the values of the column containing the list which is stored as a string. For each item in the comma-delimited list, your function should return a row in the table result. When you are using a query like this, query against the results returned from the UDF.
Write a function to convert a comma delimited list to a table. Should be pretty simple. Then you can use IN().