Imagine that you have a column with a bunch of fields like this:
ab
abc
abcd
...
I would like to transform these fields using a single query to something like this:
a_b
ab_c
abc_d
...
That is, inserting an underscore just before the last letter.
I could do it in a one by one basis:
update test set name = 'a_b' where name ='ab';
update test set name = 'ab_c' where name ='abc';
update test set name = 'abc_d' where name ='abcd';
But being a high number of fields to update, I figure there must be a better way to do this.
Try this query -
UPDATE test SET name = INSERT(name, LENGTH(name), 0, '_');
Use some of the string manipulation functions, something like:
SET fieldname = CONCAT(LEFT(fieldname, LENGTH(fieldname)-1),"_",RIGHT(fieldname,1))
You could use the SUBSTRING AND LENGTH functions:
UPDATE test
SET name = SUBSTRING(name, 0, LENGTH(name) - 1) + '_' + SUBSTRING(name, -1)
Related
I have strings that look like this:
#N05/3835412983/academy-of-sciences.html
Where 3835412983 and academy-of-sciences.html will be different throughout the database table called wp_4_posts in a column called post_content.
Is there a wildcard search that I can do to preserve the unique id between the two / and strip *.htm*? Everything to the left of the #05 cannot change. The numbers between the / need to be preserved. And the item after the second / can be stripped entirely.
Doing it manually and there's hundreds that need this done to.
For the above, the end result would be:
#N05/3835412983/
And that result would be written back into the database.
If you want to extract the middle element, then you can use substring_index():
select substring_index(substring_index(str, '/', 2), '/', -1)
This extracts the second element.
Based on the edit, you seem to want:
select concat(substring_index(str, '/', 2), '/')
If you want to update the value, then:
update t
set str = concat(substring_index(str, '/', 2), '/');
update specific string then should use replace() function
Syntax:-
UPDATE table_name
SET column_name =
REPLACE(column_name,'OLD_STRING','NEW_STRING')
WHERE column_name = 'your condtition';
in your case:-
UPDATE wp_4_posts
SET post_content =
REPLACE(post_content,'#N05/3835412983/academy-of-sciences.html','#N05/3835412983/');
if you want result based on condition
UPDATE wp_4_posts
SET post_content =
REPLACE(post_content,'#N05/3835412983/academy-of-sciences.html','#N05/3835412983/')
WHERE column_name = "values";
I have a column in table which is stored in format:
{"field1":"val1","field2":"val4"}
{"field1":"val2","field2":"val5"}
{"field1":"val3","field2":"val6"}
I need to remove all field1 with values(e.g "field1":"val1","field1":"val2","field1":"val3" ) and result should be
{"field2":"val4"}
{"field2":"val5"}
{"field2":"val6"}
I am trying to acheive this via replace but stuck as in '"field1":"val1"' string val1 could be any value like null, some integer.
UPDATE emp SET col = REPLACE(col, '"field1":"val1"', '')
I am stuck due to this dynamic value of val1.
I would prefer to use the JSON_REMOVE function (MySQL) :
UPDATE emp
SET emp.col = JSON_REMOVE(emp.col, '$.field1');
You can also add a WHERE clause :
WHERE emp.col LIKE '%val6%';
References: MySQL JSON_REMOVE and MySQL JSON path
A blog post with examples: MySQL for your JSON
And a note about json path in MySQL:
Propery names in path must be double quoted if the property identifier contains interpunction (spaces, special characters, meta characters) bugs.mysql.com
You can do it like this:
SELECT SUBSTRING(Field, 1, INSTR(Field, '"field1"')) + SUBSTRING(Field, INSTR(Field, '"field2"'), LENGTH(Field)) FROM #Temp
I don't know if this works but this is the idea. (Can't test ATM)
Here is the MsSQL equivalent (works, just tested!):
SELECT SUBSTRING(Field, 0, CHARINDEX('"field1"', Field)) + SUBSTRING(Field, CHARINDEX('"field2"', Field), LEN(Field)) FROM #Temp
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 data with 6 characters like 123456 in a mysql database. I want to change the existing data like 123,456 . Is there any mysql query can do this. Because i want to change over 3000 records.
Try this, it will FORMAT your numbers regardless of the number of digits:
UPDATE yourtable
SET yourfield = FORMAT(yourfield,0)
Try this :
UPDATE yourtable
SET yourfield = LEFT(yourfield,3) + ',' + RIGHT(yourfield,3)
it will do what you asked for. Note it won't help if your field is not 6 characters long
update mytable set
mycolumn = replace(mycolumn, ',', '')
Lets say that you have the following stored in table:
{2:22}{4:5}{34:4}
I what to delete {4:5} from this string but the system dosent know what the number after the ":" is just the first one. The query looks something like this:
UPDATE tbl SET this = REPLACE(this,'{4:??}','') WHERE id = 1;
What do i need to put in ?? place to return the following result?
{2:22}{34:4}
Here's one way to do it using LEFT, SUBSTRING, LOCATE and REPLACE:
update yourtable
set yourcolumn =
replace(yourcolumn,
Left(
Substring(yourcolumn,
Locate('{4:',yourcolumn),
Length(yourcolumn)),
Locate('}',Substring(yourcolumn,
Locate('{4:',yourcolumn),
Length(yourcolumn)))),
'')
SQL Fiddle Demo