i need to add a special text to all rows in my mysql table ,
how to add some text to the end of all rows' content in a table just for one field
i used this code :
UPDATE `blogs` SET `title`= `title` + 'mytext';
but didnt work for me
UPDATE blogs SET title=concat(title, 'mytext');
MySQL does not have a string concatenation operator (+). You have to use the concat() function, as Daniel Schneller pointed out in the other answer.
If you try SELECT '1' + '2'; in MySQL, it would return 3. The + operator is simply an addition operator. Your query would have updated the title field with a 0.
Related
I need to write a query that appends a "+" on the front of every p2_number meta_key that doesn't already begin with a "+". The name of the table is "wp_4_postmeta". I attached an image of the database so you can see what I'm talking about. http://mmw-file-sharing.s3.amazonaws.com/2015/Screen%20Shot%202015-07-30%20at%204.12.25%20PM.png
UPDATE aTable
SET someField = CONCAT('+', someField)
WHERE someField NOT LIKE '+%'
;
If someField is also indexed, the query should be fairly quick as well.
Update table set column = concat("+", column) where column not like "+%"
Should do it
You can check using substr if the first character is a +. If so, return just the field value, else prepend a +.
select
case when substr(p2_number, 1, 1) = '+' then
p2_number
else
concat('+' p2_number)
end as p2_numberplus
from
wp_4_postmeta
Or do you mean actually updating the table data? In that case, Uueerdo's answer is the one you want.
I have a table where one field named "version" contains the string "MyProgram nnnnnn".
I now wish to replace these strings so that they are only "nnnnnn", thereby remove the prepending "MyProgram ".
Is this possible, and if so, how would I do this?
If the pattern is "MyProgram nnnnnn" maning string like VB 1.3, Mysql 5.6, PHP 5.4 etc then you can do the following
update tablename
set col = substring_index(col,' ',-1)
Use Replace function,
SELECT VERSION,
REPLACE(VERSION,'MyProgram nnnnnn','nnnnnn')
FROM FROM tablename
Check this Manual
SELECT REPLACE(VERSION, 'MyProgram ', '')
FROM tablename
You can use the Replace() function for MySQL
update table set columnname = REPLACE(columnname, 'MyProgram ', '');
You want:
I now wish to replace these strings so that they are only "nnnnnn"
Shortest solution:
SELECT 'nnnnnn'
Specify your pattern of the content of this column!
SELECT VERSION,
REPLACE(VERSION,'MyProgram nnnnnn','nnnnnn')
FROM FROM tablename
Regards,
Praveen Nelge
I'm needing to do a very important database string correction on 192 rows and am wondering if this is correct syntax:
UPDATE `DATABASE_NAME`.`TABLE_NAME` SET `FIELD_NAME` = REPLACE(`FIELD_NAME`,`REPLACE_THIS_STRING`,`WITH_THIS_STRING`);
Thanks in advance!
The best way to find out is to write it as a SELECT statement first to "preview" the results.
SELECT field_name As before
, Replace(field_name, 'replace this string', 'with this string') As after
FROM table_name
Optional WHERE clause (to only affect the rows that contain our replacement string):
...
WHERE field_name LIKE '%replace this string%'
Well, i would write it like this
UPDATE `DATABASE_NAME`.`TABLE_NAME` SET `FIELD_NAME` =
REPLACE(`FIELD_NAME`,`REPLACE_THIS_VALUE`,`WITH_THIS_VALUE`);
Does the table only contain these 192 rows? Otherwise you must add a WHERE to the syntax. Or it will update all FIELD_NAME rows.
As #gvee suggest, try to select the rows first and see how the result looks like to ensure the update scope is correct
I have a column in my table named title_id with some title ids. Each title id is stored like
title_1
title_2
title_3
where 1, 2, 3 are the actual ids of the titles.
I want to remove the " title_ " text from all the records. I have around 5000 records so I can't edit them manually.
How can I do it with a query.
Thanks in advance
Update table_name set `title_id` = REPLACE(`title_id`,'title_','');
I didnt' tested it . Please check
UPDATE table_name SET title_id = REPLACE(title_id, 'title_','')
Check out the REPLACE function. You can do something like:
UPDATE table
SET title_id = REPLACE(title_id, 'title_', '');
(Ah, and be sure to first test your UPDATE query by running a SELECT query!)
Try something like
update table_name set id=substring(id, from length('title_'))
where id like 'title%';
I didn't test this, because I have no MySQL DB available here. The syntax for the substring function is from the MySQL docs.
I have a table of users which has a username column consisting of a six digit number e.g 675381, I need to prepend a zero to each of these usernames e.g. 0675381 would be the final output of the previous example, is there a query that could handle this?
UPDATE Tablename SET Username = Concat('0', Username);
what type is the column of?
if it's string type, try something like this:
UPDATE your_table SET column_name=concat('0',column_name);
You mean "prepend" ? i.e. add it on the front?
Is the column numeric? Do you always want 7 characters output?
Assuming that, something like this would work for a query:
select LPAD(CONVERT(username, CHAR), 7, '0')
If the column is characters, the CONVERT() part is unnecessary, just LPAD the username.
If you want to permanently modify the value in the table, you'll need to ensure the column is a character type and UPDATE using the above.
You might want to use CONCAT_WS('', '0', Username) because if there is a null value, then you'll end up with NULL instead of '0'. This probably isn't a problem, but something I've learnt the hard way.