Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Renaming Filenames In Table

Tags:

sql

mysql

I have a table as

'310', 'D', '1', '0', 'Clowns.jpg', ?, '63527560196'
'311', 'D', '1', '1', 'Clowns_102_x_102.jpg', ?, '63527560197'
'312', 'D', '1', '1', 'Clowns_45_x_45.jpg', ?, '63527560197'
'313', 'D', '1', '1', 'Clowns_80_x_80.jpg', ?, '63527560197'
'314', 'D', '1', '1', 'Clowns_120_x_120.jpg', ?, '63527560198'
'315', 'D', '1', '1', 'Clowns_180_x_180.jpg', ?, '63527560198'
'316', 'D', '1', '1', 'Clowns_300_x_300.jpg', ?, '63527560198'

I want to rename the files called Clowns to Clowning so the data would look like this

'310', 'D', '1', '0', 'Clowning.jpg', ?, '63527560196'
'311', 'D', '1', '1', 'Clowning_102_x_102.jpg', ?, '63527560197'
'312', 'D', '1', '1', 'Clowning_45_x_45.jpg', ?, '63527560197'
'313', 'D', '1', '1', 'Clowning_80_x_80.jpg', ?, '63527560197'
'314', 'D', '1', '1', 'Clowning_120_x_120.jpg', ?, '63527560198'
'315', 'D', '1', '1', 'Clowning_180_x_180.jpg', ?, '63527560198'
'316', 'D', '1', '1', 'Clowning_300_x_300.jpg', ?, '63527560198'

So I tried this

UPDATE new_images 
SET 
filespec = 'clowns100' + MID(filespec, 7, LENGTH(filespec) - 6);

However I got

Error Code: 1292. Truncated incorrect DOUBLE value: 'clowns100'

like image 452
Stephen Bouffe Avatar asked Jan 24 '26 15:01

Stephen Bouffe


2 Answers

use REPLACE

UPDATE new_images SET filespec = REPLACE(filespec, 'Clowns','Clowning')
  • MySQL Replace (String Function)

UPDATE 1

the equivalent mysql code for your query is

UPDATE new_images 
SET filespec = CONCAT('clowns100', MID(filespec, 7, CHAR_LENGTH(filespec) - 6));
like image 155
John Woo Avatar answered Jan 26 '26 06:01

John Woo


You can use substring in SQL

 UPDATE new_images SET filespec = 'Clowning'+substring('Clowns.jpg', 7, len('Clowns.jpg') - 6) where filespec like 'Clowns%'
like image 34
Shafeeq Koorimannil Avatar answered Jan 26 '26 07:01

Shafeeq Koorimannil



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!