php - Adding zeros to specific place in mySQL table row -


i have mysql table alot of links this:

id - link 1 | index.php?video=12 2 | index.php?video=345 3 | index.php?video=6789 4 | index.php?video=123&other=variable 5 | www.site.com/index.php?video=456&other=variable 

one link per text row. add zeros before numbers has 9 numbers in total. video=12 video=000000012 , video=6789 video=000006789.

is there way acheive using sql query?

edit: solution tombom submitted worked fine if have links don't have video=x variable?

update yourtable set `link` = replace(`link`, substring(`link` locate('=', `link`) + 1), right(concat('000000000', substring(`link` locate('=', `link`) + 1)), 9)) 

see working live here in sqlfiddle.

update:

what if have links more url variables? like: index.php?video=123&play=1&search=hello

that's bit trickier, here go:

update yourtable set `link` = replace(`link`, substring(`link`, locate('=', `link`) + 1, abs(locate('&', `link`) - locate('=', `link`) - 1)), right(concat('000000000', substring(`link`, locate('=', `link`) + 1, abs(locate('&', `link`) - locate('=', `link`) - 1))), 9)) 

or can bit shorter this:

update yourtable set `link` = , concat(substring_index(`link`, '=', 1),'=', lpad(substring(`link` locate('=', `link`) + 1),9,'0')) 

see sqlfiddle.


Popular posts from this blog

How to calculate SNR of signals in MATLAB? -

c# - Attempting to upload to FTP: System.Net.WebException: System error -

ios - UISlider customization: how to properly add shadow to custom knob image -