Change image name and update into same table using mysql query - mysql

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

Related

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%';

remove part of string record in 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/');

Replace Content using MySQL

In many of my column I have some data like this :
"Some Data. [content]Some Content[/content] Another Data"
I want to remove the substring which starts with [content] and ends with [/content]. The result should be :
"Some Data. Another Data"
I know it is possible by using MySQL replace() with Reg Ex but I don't how to use it.
Any help would be appreciated.
first get all the string between that string and after that you can able to replace it:
SELECT substring_index(substring_index(field, 'content', -1), '/content', 1) FROM table_name
after run this query, you get between string of [content] and [/content]
after that you just have to update one bye one using foreach loop with use of this query
UPDATE table_name SET field = REPLACE(field, 'content', 'content-that-will-replace-it') WHERE INSTR(field, 'content') > 0

Codeigniter database prefix add in table

I have one database in which all table names like below
configuration_dst,
developer_dst,
application_dst
Now I want to manage my query in which want to add database prefix after table name instead of before.
For example :
{TABLE NAME}{PREFIX}
is it possible to manage using CI 3.0 ?
I have tried like below in Application/configuration/database.php
$db['default']['dbprefix']="_dst";
$db['default']['swap_pre']="{POST}";
Current Query :
$this->db->get('templates');
Current Table Name :
tablename : _dsttemplates
Expected Table Name :
tablename : templates_dst
I need prefix after table Name not before but didn't get any solution.
The only option is to change in driver files. You can do this using the following steps:
Go to /system/database/DB_query_builder.php
Search public function dbprefix
Replace
return $this->dbprefix.$table;
with
return $table.$this->dbprefix;
If it also interferes in any other places in your project then create a new function with replaced code like:
public function dbsuffix($table = '')
{
if ($table === '')
{
$this->display_error('db_table_name_required');
}
return $table.$this->dbprefix;
}
Working with Database prefixes manually
if you use
$this->db->set_dbprefix('newprefix');
$this->db->dbprefix('tablename'); // outputs newprefix_tablename
its always gives the table name as
newprefix_tablename
Because this is Codeigniter pastern.
Codeigniter Prefix

mysql update (i want to change the string last two character)

i have a string
www.baabrada.aapnipanchayat.org
i want to change the string
www.baabrada.aapnipanchayat.in
and i have use this update query
UPDATE `gram_panchayat` SET web=in WHERE web=org;
Ensures what .org be replaced with .in only at the end of string:
UPDATE gram_panchayat
SET web = CONCAT(LEFT(web, CHAR_LENGTH(web)-CHAR_LENGTH('org')), 'in')
WHERE web LIKE '%.org'
Test it: http://sqlfiddle.com/#!2/e78a26/1
More info: String Functions
Another way to do this would be to use the INSERT() function:
UPDATE gram_panchayat
SET web = INSERT(web,
CHAR_LENGTH(web) - CHAR_LENGTH('org') + 1,
CHAR_LENGTH('org'),
'in'
)
WHERE web LIKE '%.org';
Here's a SQL Fiddle demo to play with: http://sqlfiddle.com/#!2/524905/1