This question already has answers here:
MySQL search and replace some text in a field
(7 answers)
Closed 5 years ago.
I have a table, and in one colu,m I store comma separated strings, like:
book, table, lamp (New)
need to write query loop through all the strings and remove
(New)
I think I can do it by first running a query to fetch results containing (New), then using PHP's REGEX, remove (New) and then update the the same row with new string. However this sounds a bit convoluted. Isn't there a way to do this with a single query?
There is a replace string function in mysql: https://dev.mysql.com/doc/refman/5.7/en/string-functions.html#function_replace
UPDATE <table> SET <column> = REPLACE(<column>,'(NEW)','')
Try it with select first to see if the result is what you want (you may want to remove the trailing space if new is always preceded by it)
Related
This question already has answers here:
Update a column value, replacing part of a string
(6 answers)
Closed 2 years ago.
Details:
I have stored physical paths into MySQL table. I have moved all content to a new folder.
This is the data in the database:
Current data---
g:\Folder1\File 1.jpg
g:\Folder1\Excel File.xlsx
g:\Folder1\Test.js
Desired change:
Here is what I'd like to achieve, add an extra folder to the path before the filename.
Desired---
g:\Folder1\New Folder\File 1.jpg
g:\Folder1\New Folder\Excel File.xlsx
g:\Folder1\New Folder\Test.js
Question
How can I achieve this? And just for future, how can I remove a specific folder from the path?
Simply use REPLACE as explained also in this SO question
UPDATE table
SET fied = REPLACE(field, 'g:\Folder1\', 'g:\Folder1\New Folder\')
It is untested so you may have to fix the \ escaping
Since REPLACE gives you the ability to replace the string with another one you can just modify the strings to "add or remove folders"
Use replace function:
SELECT replace(filename, "Folder1\\", "Folder1\\New Folder\\")
FROM your_table;
You can specify a path from drive letter to avoid random data being replaced.
This question already has answers here:
MySQL search and replace some text in a field
(7 answers)
Closed 5 years ago.
I have some records in MYSQL database with domain name, Now i changed domain and i want use query for find these word example.com in record column and then change to example.net.
I think if column only include domain, it's very easy but my records include domain name and some text. Like this:
it's a sample text www.sample.com and i want change it
I have some recorde like above and i want find every recorde that have sample.com text and then change it to sample.net
You can use a simple REPLACE() for this:
UPDATE <table> SET <column> = REPLACE(<column>, 'www.sample.com', 'www.example.net');
The above will replace all occurances of www.sample.com with www.example.net in the specified column.
UPDATE mytable SET mycolumn = REPLACE(mycolumn,'www.sample.com','www.sample.net');
Replace
This question already has an answer here:
Escaping a single quotation within SQL query
(1 answer)
Closed 7 years ago.
I have ruby script that includes a mysql insert that is working fine until it gets to a row that contains data containing an apostrophe. This row is also being populated using a variable and Im unsure how to escape the character so the insert will work successfully.
Any ideas?
Use the quote method on the connection object:
quote(value, column = nil)
API Documentation Link
Quotes the column value to help prevent SQL injection attacks.
Example:
my_name = ActiveRecord::Base.connection.quote("John O'Neil")
my_address = ActiveRecord::Base.connection.quote("R'lyeh")
query = "INSERT INTO companies (name,address) VALUES (#{my_name}, #{my_address})"
ActiveRecord::Base.connection.execute(query);
Original Post:
See this post: Escaping a single quotation within SQL query
This question already has answers here:
How to do a regular expression replace in MySQL?
(13 answers)
Closed 6 years ago.
I have a table which needs stores some name and i need to replace a few characters before comparing them with another string
For instance, My table data is
abc
ghi:dki
ioe dsa
i read a string from user, which is of the form abc, ghi-dki, ioe-dsa. ie, all blankspaces, multiples spaces and symbols are converted to a hyphon(-). Now i need to compare. something like
SELECT MYCOLUMN FROM MYTABLE WHERE {Converted MYCOLUMN} = 'ghi-dki'
Can someone help me for figuring out which MySQL function can do it?
You can't do a regex replace in MySQL, but you can do a match.
SELECT mycolumn FROM tablename WHERE mycolumn REGEXP Replace('ghi-dki', '-', '^[\s:_-]*$');
Note: I didn't completely fill out the symbols character set, you'll have to add whatever you're using.
This question already has answers here:
MySQL variable format for a "NOT IN" list of values
(3 answers)
Closed 8 years ago.
In filtering out some spam, I have two MYSQL statements in one file,
SET #valid_users := '"admin", "jrock", "kmicka", "First Last"'; //etc
Followed by a SELECT like this
SELECT /*stuff*/ /*WHERE*/ /*filters*/ AND User.user_name NOT IN (#valid_users)
When I do this, it acts as if #valid_users is an empty string. (Returns all results). But if I change the clause to NOT IN ('admin', 'jrock', etc) then it works as it should.
Why would a variable in the NOT IN filter not work?
You'll want to take a look at MySQL's find_in_set() function:
SELECT
*
FROM
your_table
WHERE
NOT FIND_IN_SET(User.user_name, #valid_users);
For this to work, the comma-separated list shouldn't contain quotes (unless your usernames actually contain quotes) and should not be padded with spaces:
SET #valid_users := 'admin,jrock,kmicka,First Last';
SqlFiddle Example
To directly answer your question regarding "why would a variable in the NOT IN filter work", it's because #valid_users is being treated as a string and when you pass it to IN(), it's being treated as a single string (i.e. not a set/list). With FIND_IN_SET(), it treats the string in #valid_users as a comma-separated set/list and uses it accordingly.