How to delete specific value from a column in SQL row? - mysql

I am a beginner in SQL. Currently, I am working with a SQL database that has two columns. The first column specifies the id. The second column specifies a list of people separated by the delimiter "#d#" So, the column looks something like "John#d#Jack#d#Prince"
I need to delete a specific name from this list. Suppose, I am deleting prince from the list. I want my row to look like John#d#Jack after the delete operation. I was researching solutions for this procedure and I found couple resources. I learned about this approach "UPDATE TABLE SET columnName = null WHERE YourCondition" As a result, I can change the whole column to null, but I don't know how to retain the string and only delete the specified value.

You can use replace function
update yourTable set yourField = replace(replace(yourField, 'Prince', ''), '##' , '#') where yourCondition;
First replace "delete" the name you want to, second replace "delete" deleted name's delimiter.

You can do this using:
update t
set list = trim(both '#' from replace(concat('#', list, '#'), concat('#', 'prince', '#'), '#'))
where concat('#', list, '#') like concat('%#', 'prince', '#%');
You can replace 'prince' with a variable or whatever you want to replace.

If I am not mistaken the command you are looking for is
UPDATE TABLE set columnName = "John#d#Jack" WHERE YourCondition
Or do you want a more general approach?

Related

How to I add a column to a table, that combines strings from other columns?

I am using Mysql Browser.
I have a table called "users". I am looking to create a column called, "opp". I know how to do this, with a simple Alter Table, Add statement. Where I am struggling is figuring out how to add data to the column. I want the column to be a combination of strings using concat().
The column should end looking like:
"Team(name)" where name is data from a column titled "name" in the table "users".
I have tried a Insert into statement like this:
Insert Into users (opp)
VALUES ('Team', '(', name, ')')
But that did not work. It tells me there is no such column as "name", even though there is...
Thanks for the help!
Once you have altered the table you should use an UPDATE
update users
set opp = concat( 'team(',name ,')' )
;
anyway column like this should not be store .. because you can obatin this with simply
select concat( 'team(',name ,')' ) opp from user
Why bother adding a column? You can use a view:
select u.*, concat('team('name , ')') as opp
from u;

Mysql update rows dynamically based on column input

I have the following issue. I have a table called titles with the following structure:
id int(10),
name varchar(100),
At some point later we added a new column called modified_name. It is defined as the same as name except that it is lower case and has all of the spaces replaced with a -. We added this column and so we needed to now get the right modified name value into each record of that column. To do this we wrote a PHP script that handled that by loading in values from the database and processing them, but that is highly inefficient. Is it possible to write a single UPDATE query that would add the correct value to each record in the titles table. I can think of ways to do this with a stored procedure and a while loop there in, but I want to know if something more efficient is possible. It there any way to achieve something like the following:
UPDATE `titles`
SET
`modified_name` = LOWER(REPLACE(SELECT `name` FROM `titles` WHERE id = PRESENT_VALUE), ' ', '-');
The goal being to SET the modified_title column of every record in the titles table to a unique value that results from that record's name column as followed:
# Before modification update query
name = "Hello Goodbye"
modified_name = ""
# After modification update
name = "Hello Goodbye"
modified_name = "hello-goodbye"
Thank you for your help, any advice on how best to do this would be appreciated.
UPDATE `titles` SET `modified_name` = LOWER(REPLACE(`name`, ' ', '-'))

Remove a single character from a varchar field SQL Server 2008

I have a table with several varchar columns that are almost identical to primary keys I have in another table, with the exception of a period (.). I've looked at the replace function in T-SQL but the first argument isn't an expression. How can I remove all occurrences of a particular character with SQL? It seems like the correct answer might be to replace with a zero length string. Is that close?
To whomever felt the question didn't exhibit research effort it was mainly due to a misunderstanding of the documentation itself.
You can update the table directly using REPLACE on the column values:
UPDATE myTable
SET myColumn = REPLACE(myColumn, '.', '')
Do you want to remove all instances of the . from the string? If so, you were right about REPLACE:
DECLARE #Example TABLE
(
Value VARCHAR(100)
)
INSERT #Example (Value)
VALUES ('Test.Value'), ('An.otherT.est')
SELECT
REPLACE(Value, '.', '')
FROM
#Example
-- Replace only the first '.'
SELECT
STUFF(Value, CHARINDEX('.', Value, 0), 1, '')
FROM
#Example
Edit, making the example a little more useful since I played around with it anyway, I might as well post the example. :)
update your_table
set some_column = replace(some_column, '.', '')

REPLACE statement with embedded IF() logic?

EDIT: This actually works fine, no idea why I thought otherwise.
I have a prices table which includes a column price_was which needs to contain the highest ever value for prices.
Is it possible to do a REPLACE query which would update this if required?
The following (which is simplified and built dynamically in PHP) doesn't seem to work.
REPLACE prices
SET price = 1.99,
price_was = IF(1.99 > price_was, 1.99, price_was)
id_product = 1
I'm thinking perhaps it's not possible, but would love to hear otherwise since I'm updating many records and need to be as efficient as possible.
The query you posted is indeed valid, try it for yourself. I would use an UPDATE though since you're only updating one field and the REPLACE can possible over-write other column data you want left alone.
Try INSERT ... ON DUPLICATE KEY UPDATE instead:
INSERT INTO prices (price, price_was, id_product)
VALUES (1.99, 1.99, 1)
ON DUPLICATE KEY UPDATE
price_was = IF(VALUES(price) > price_was, VALUES(price), price_was)
id_product = VALUES(id_product)
This will do either an INSERT or an UPDATE, while the REPLACE statement does either an INSERT or a DELETE followed by an INSERT. You are not able to reference old values in a REPLACE statement, probably because of the DELETE/INSERT semantics. From the docs:
Values for all columns are taken from
the values specified in the REPLACE
statement. Any missing columns are set
to their default values, just as
happens for INSERT. You cannot refer
to values from the current row and use
them in the new row. If you use an
assignment such as SET col_name =
col_name + 1, the reference to the
column name on the right hand side is
treated as DEFAULT(col_name), so the
assignment is equivalent to SET
col_name = DEFAULT(col_name) + 1.

MySQL update with same table values

I have a MySQL table named "users" that has the columns "firstname" and "surname". There are a thousand or so rows in the table.
I have now added another column named "search" and I would like to populate it with the values of both the first name and the surname separated by a space. For example, if firstname = Bob and surname = smith, i would like to populate "search" with "Bob Smith".
Can anyone advise on an update statement that selects these values and inserts them in to the new column?
Best regards, Ben.
You could simply use...
UPDATE users SET search=TRIM(CONCAT(firstname, ' ', surname));
As an explanation, CONCAT simply concatenates (merges) the supplied fields/values, and TRIM will remove any leading or trailing spaces, hence ensuring that there are no issues if a firstname/lastname or indeed both are missing. See the MySQL String Functions manual page for more information on these functions.
However, I'd be tempted to call the new column "name" (or indeed "full_name") rather than "search", as this is, at best, a somewhat misleading field name.
update users
set search = CONCAT(firstname, ' ', surname)
This script will update it.
I'm not sure about MySql, but Sql Server lets you set a field as a calculated column. You can specify the calculation as "ISNULL(FirstName, ' ') + ' ' + ISNULL(Surname, ' ')" and set it as persisted so it doesn't calculate it each time. In that case, you wouldn't need an update script
update `users` set `search` = CONCAT(`firstname`, ' ', `surname`);