remove part of string record in mysql - mysql

this is my string record on tables in mysql
"ftp://myftp.co/ftp/Media_Gallery//Cartoon/Serries/Shaun_The_Sheep/Shaun_The_Sheep_E015.mkv"
now i want remove "//" at the center of my string after "Media_Gallery"
but when i use replace queries // this query remove // at the first of URL and its wrong
my string after run query would be this scheme:
"ftp://ftp.um.ac.ir/ftp/Media_Gallery/Cartoon/Serries/Shaun_The_Sheep/Shaun_The_Sheep_E015.mkv"

UPDATE Mytable SET name = REPLACE(name, 'y//', 'y/');

Related

Remove All data in string after 10th entry

I have this data in a string 0871234567ThisPartOfTheStringIsRandom
How Do I update the string to just keep the first 10 Chars?
Please Keep in mind I have thousands of entries where 'ThisPartOfTheStringIsRandom' is different in every case
The LEFT function is a string function that returns the left part of a string with a specified length.
UPDATE TableA
SET YourColumn = LEFT(YourColumn,10)

prepend a string to a column value before specific character in MySQL

Imagine I have a table which has a Params column and it contains settings of my gallery. I want to add 2 other properties automatically with MySQL query.
I have already test this query but it will add my new properties at the first of the column value which is incorrect :
UPDATE table
SET params=CONCAT(', "slider_fullscreen_button_skin": "myTheme", "slider_zoompanel_skin": "myTheme"',params)
WHERE params NOT LIKE '%myTheme%';
let's say I have this value on my column :
{"title":"Bessariabian","alias":"Bessariabian","category":"1103","full_width":"false"}
I want my value change to something like this:
{"title":"Bessariabian","alias":"Bessariabian","category":"1103","full_width":"false", "slider_fullscreen_button_skin": "myTheme", "slider_zoompanel_skin": "myTheme"}
I want to make a query which adds the new properties at the end of the value before } location.
How I can handle this?
Use Substring_Index() function to get the Substring before the first occurence of }.
Now, Concat() this substring with your required string, and } at the end.
Try:
UPDATE table
SET params = CONCAT(
SUBSTRING_INDEX(params, '}', 1),
', "slider_fullscreen_button_skin": "myTheme", "slider_zoompanel_skin": "myTheme"',
'}'
)
WHERE params NOT LIKE '%myTheme%';

Append a string value to an exisiting json value in mysql

I'm new to MySQL. I'm trying to add a string value to a json value in MySQL. The column name is IPConfig. This is the current json string in the column.
{"theme":"black", "button1link":"http//sample.com", "name":"pp"}
I have to append a "www" to button1link value.
Thanks in advance!
Here you can try
UPDATE table SET DATA= JSON_SET(DATA, "$.button1link", INSERT("http//sample.com", 7, 0,'www')) WHERE 1 = 1;
But for this to work, you will need MySQL 5.7+
You can have insert function docs here.

Incrementing numerical value and changing string in SQL

I have a database that has stored values in a complicated, serialized array where one component is a string and another is the length of the characters of the string, in this format:
s:8:"test.com"
Where "s" holds the character length of the string in the quotations.
I would like to change the string from "test.com" to "testt.com", and I'm using the following statement in SQL:
UPDATE table SET row=(REPLACE (row, 'test.com','testt.com'))
However, this breaks the script in question, because it doesn't update the character length in the "s" preceding the string where "test.com" is stored.
I was wondering if there is a query I can use that would replace the string, and then also increment the value of this "s" preceding to where the replacement occurs, something like this:
UPDATE table SET row=(REPLACE (row, 's:' number 'test.com','s:' number+1 'testt.com'))
Does anyone know if this kind of query is even possible?
UPDATE table set row = concat('s:',length('testt.com'),':"testt.com"');
If you need to change exact string, then use exact query -
UPDATE table SET row = 's:9:"testt.com"' WHERE row = 's:8:"test.com"';
The string is a "serialized string".
If there are multiple strings to be replaced, it might be easier to create a script to handle this.
In PHP, it goes something like this:
$searchfor = serialize('test.com');
$replaceby = serialize('testt.com');
// strip last semicolon from serialized string
$searchfor = trim($searchfor,';');
$replaceby = trim($replaceby,';');
$query = "UPDATE table SET field = '$replaceby' WHERE field = '$searchfor';";
This way, you can create an exact query string with what you need.
Do fill in the proper code for db connection if necessary.

Change image name and update into same table using mysql query

I want to change image name in table like below.
image name : test.png
replace with : test_E.png
I want _E at end of all image name in table using mysql query.
Use replace function
update <table>
set image=replace(image,'.png','_E.png')
you could use this, if the image extension is not same in the table
update <table>
set image=concat(substring(image,1,locate('.',image)-1),'_E',
substring(image,locate('.',image),lenght(image)))
You can use string functions of MySQL query:
UPDATE TABLE SET IMAGE_NAME = CONCAT(SUBSTR(IMAGE_NAME,(CHAR_LENGTH(IMAGE_NAME) - 4)),
'_E' , SUBSTR(IMAGE_NAME, -4)) WHERE ID = <put record id>;
SUBSTR(IMAGE_NAME,(CHAR_LENGTH(IMAGE_NAME)-4)) would return name of file - assuming extension is of 3 chars. For 'test.png' above function would remove '.png' and function would return 'test'
SUBSTR(IMAGE_NAME, -4) would return last four chars of string - so 'test.png' would return '.png'
using concat you can concat 'test', '_E' and '.png' - returning 'test_E.png'
Please refer to string functions reference of MySQL for further use
http://dev.mysql.com/doc/refman/5.0/en/string-functions.html