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','')
Related
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?
I need a way to check a database if a word is in it already if so then it doesn't have to be pushed to the database if the word isn't in it yet then it has to be pushed into it.
It's a MYSQL database and I have to do it in PHP this is what I got so far.
$result = array_count_values(explode(" ", $filter));
arsort($result);
foreach ($result as $word => $frequency)
{
if (!in_array($word, [" ", ""]))
query("words", "INSERT INTO Woord (woord) VALUE (?)",[$word], false);
}
query("words" "SELECT WHERE")
You have 2 options:
REPLACE
REPLACE INTO table
SET column = 'example'
This will overwrite if the record exists and if not it will create it.
INSERT IGNORE
INSERT IGNORE INTO table
SET column = 'example'
This will ignore the query if already exists and if not it will create it.
Your php query should look like this:
"INSERT IGNORE INTO ID142118_ascii.Woord (woord) VALUES (".$word.")"
put a unique constraint on the column "woord" in the table.
Then you can let your php script insert as many duplicate words as you want to, it will simply fail.
you could either add a part "ignore duplicate" in your query or just ignore the error you will get.
I thinks that will be easiest to do.
edit:
btw I can think of a lot of words containing serveral of the character you are stripping: "foto's", "zee-eend" etc
--
how to make a unique index:
ALTER TABLE asciiwoorden
ADD UNIQUE INDEX somename (Woord);
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.
Ok I know my database is not normalized but in my case, It is not possible to normalize the data as each user has different access level with access rights, everytime I modify something and I need to delete the rows and insert back so I went for comma-separated values, here the issue is if I remove a particular group, regex doesn't work, it throws me an error..am using php and the error goes like this
FUNCTION database_name.REGEXP_REPLACE does not exist
Table structure
allowed_group_ids
+----------------+
12345,34345,55454
My query, say for example $delete_id is 12345 or say 55454, I only pass one ID at a time and the id's has no space in between and it's a text field
UPDATE tbl_scripts SET allowed_group_ids = TRIM(BOTH ','
FROM REGEXP_REPLACE(allowed_group_ids, '(,(\s)?)?$detele_id', ''))
WHERE system_id = {$_SESSION['system_id']}
So what's wrong in here?
There is no such thing as regexp_replace in MySQL.
You could perform this query using regular replace:
UPDATE tbl_scripts SET allowed_group_ids =
trim(BOTH ',' FROM
replace(
replace(allowed_group_ids, '$delete_id',','),
',,',
','
)
)
WHERE system_id = {$_SESSION['system_id']}
This first removes the ID, and removes any doubled-up commas, then removes any commas at the start/end of the string. This should be sufficient but you can add another replace to remove whitespace if necessary.
Update: SQL Fiddle shows the query in action
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 = ''