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.
Related
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)
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/');
I have a table that has some rogue tags that need replacing
The offending string ends <tr> and needs replacing with </table>
Not all record are affected, so I need to find these and then replace them
Our skills using Update Replace Where are limited as the characters are not unique within the string but their position is, ie the last 4 characters
Have tried using
UPDATE table
SET field
REPLACE (RIGHT(field,4),</table>)
but suspec this is over simplified (and also fails)
try this:
UPDATE table
SET field=concat(left(field,length(field) -4),'</table>')
I had a similar situation in which needed to replace '_' from end of the transaction number field, where there where more than one occurrences of _ in field. Example: 20161124_C_BGN_5570.77_ & 20161121_C_HRK_1502360000__
Solution:
UPDATE temp
SET transaction = LEFT(transaction, LENGTH(transaction) -1)
WHERE RIGHT(transaction, 1) = '_';
// in case of double underscore (__)
UPDATE temp
SET transaction = LEFT(transaction, LENGTH(transaction) -2) # WHERE id = xxx WHERE RIGHT(transaction, 2) = '__';
I have the following query attempting an update in CodeIgniter:
$sql = "UPDATE fanout.manual_data
SET call_leader_id = ?
WHERE id IN (?)";
$q = $this->db->query($sql, array($leaderID, implode(", ", $empIDs)));
The implode is creating a string of all the IDs in my array. However, that is resulting in the query looking like:
UPDATE fanout.manual_data SET call_leader_id = '55993' WHERE id IN ('57232, 0097726, 0076034');
When what I need is:
UPDATE fanout.manual_data SET call_leader_id = '55993' WHERE id IN (57232, 0097726, 0076034);
Only difference, is the single quotes surrounding the string of IDs. Is this something I need to do myself and skip over CI's query bindings (http://codeigniter.com/user_guide/database/queries.html) or is something CI can handle and I'm just missing a step?
Thanks.
I don't think you can skip that behavior. You're technically passing a string, so CI interprets it as such and simply surrounds it with quotes.
I think you're better off simply concatenating the $empIDs by hand (e.g. using a foreach loop), escaping them with $this->db->escape() in case you wanna be sure.
Hi is it possible to cut string like this:
String in "data" columns: ,123,456
Cut the first character i.e "," (comma).
So the query is something like:
Update users set data = cut first string...
UPDATE users SET data = SUBSTR(data, 2);
This will iterate through all rows in users and replace data with itself minus the first character.