I have names in my database like Ætherling or other words where the AE can be in the middle of a string. I'd like to change all instances of Æ to AE. All strings in my database are utf8_unicode.
This is what I currently have:
UPDATE `cards` set name = REPLACE(name,'Æ','AE');
However this only does it for the particular column. How can I do the entire table in MySQL?
I don't think it have much sense to run query that will update ALL columns, as some of them might have not contain varchar values.
You must specify your fields explicitly:
UPDATE `cards` set
name = REPLACE(name,'Æ','AE'),
other = REPLACE(other,'Æ','AE'),
andother = REPLACE(andother,'Æ','AE');
Or you can make use of query from here:
Replace all fields in MySQL
select concat(
'UPDATE my_table SET ',
column_name,
' = REPLACE(', column_name, ', ''a'', ''e'');')
from information_schema.columns
where table_name = 'my_table';
It will generate a set of update queries. You copy them, paste them and run them.
You could always dump (export) the table contents, getting a create and insert command that you paste into a text editor, replace all Æ-s to AE, drop the table and run the exported script that wil re-create it with the changes you made.
Related
Right now Im using the following script to find and replace text in a MySQL database using phpmyadmin and it works fine.
For a single table update
UPDATE 'table_name'
SET 'field_name' = replace(same_field_name, 'unwanted_text', 'wanted_text')
What I want now is to find entries that contains a specified string, and then replace the whole field.
So for example, If I have the following entries:
ABC_1234
ABC_123456789
XYZ_1234
And I want to look for the entries that contains "ABC_" and replace the entire field to "FGH-432"
The result of the desired script will give the following output.
ABC_1234 > FGH-432
ABC_123456789 > FGH-432
XYZ_1234 (No change)
What modification should I make to the script?
You could do:
UPDATE `table_name`
SET `field_name` = 'FGH-432'
WHERE `field_name` LIKE 'ABC\_%';
I have some old DB where I did not put some restrictions for inserting names through web PHP form. Now I have put them there to be only single word not two words or more. There are 10 000 rows now after the years in that DB.
Example field in DB: "John Doe Blue"
With new rules user can only fill in "JohnDoeBlue"
Is there any way how to Select all cases where I have names which do not contain only single word but multiple in that string field? My new function which prevents wrong or multiple words in the field is this:
public function wrongName($name)
{
return !preg_match("/^[a-zA-Z0-9]{3,20}$/", $name);
}
How can I select from DB all cases which do not meet this function but are already existing in the DB?
If you want to remove any spaces from a column containing data like this "John Doe Blue" all you need to do is
UPDATE table SET column = REPLACE(column, ' ','');
If you want to check that you are only getting the rows you want before doing the update try
SELECT column, REPLACE(column, ' ','') as potentialNewColumn
FROM table
WHERE INSTR(column, ' ') > 0 ;
The following query could be used to replace and search.
select * from table where REPLACE (column,' ','') like '%your value%':
I have a PostgreSQL table with a text field with a default value of an empty string ('') that will be a list of tags. I am trying to update that field by appending a string using CONCAT, but it is not working. This is my SQL:
UPDATE pins SET tags = concat(tags, :tagString) WHERE id = :id
$tagString would look something like this: [5378]
Similar queries have worked like this:
UPDATE pins SET tags = :tagString WHERE id = :id
It seems that the problem is the concat function. What is wrong?
My database contains empty table columns.
I would like to add a character like § to these empty rows so that I can search for them easier. How would I go about?
I already have a script that lets me replace or remove characters but I dont know a way to specify that rows that are empty should be updated with a character.
First, you probably don't have empty rows but empty column values in the rows. Wouldn't it be better if you just do it like if (!empty($row['column'])) instead of trying to put some bogus character?
Or if you want to do a SELECT just do something like this:
SELECT * FROM table_name WHERE column_name > ''; // seems to work for both NULL and empty string
Or:
DELETE FROM table_name WHERE column_name IS NULL or column_name = '';
UPDATE `table` SET column = "§" WHERE column = "";
It's bad to add character to an empty column because you are only adding extra size to the database. It's easy to search empty string on the database. Possible solutions of searching will be using of IS NULL to search for null columns.
SELECT * FROM tableName WHERE collName IS NULL
Another is by using CHAR_LENGTH (which gets the length of the data in the column)
SELECT * FROM tableName WHERE CHAR_LENGTH(collName) = 0
or by simply comparing it to ''
SELECT * FROM tableName WHERE colName = ''
I have some rows ( > 50.000) in my database which contains columns with strings + '\n'. I mean the records looks like this:
abcd\n or \nabcd
Because of that sometimes my application does not work correctly. My question is: How could i update this rows with the correct value and eliminate '\n' spaces besides going to every row and update it manually ?
update your_table set somecolumn = replace(somecolumn, '\n', '')
update mytable SET title = TRIM(TRAILING '\n' FROM title)
Have you tried this?
UPDATE table SET column=REPLACE(column,'\\n','') WHERE column LIKE '%\\n' OR column LIKE '\\n%'
If you don't know where the new line is, simply:
UPDATE table SET column=REPLACE(column,'\\n','')