{"id":1121,"date":"2012-10-30T09:17:05","date_gmt":"2012-10-30T09:17:05","guid":{"rendered":"http:\/\/beta.robertprice.co.uk\/robblog\/?p=1121"},"modified":"2012-10-30T09:17:05","modified_gmt":"2012-10-30T09:17:05","slug":"replacing-text-in-a-mysql-database","status":"publish","type":"post","link":"http:\/\/www.robertprice.co.uk\/robblog\/replacing-text-in-a-mysql-database\/","title":{"rendered":"Replacing Text In A MySQL Database"},"content":{"rendered":"<p>There are times when you find yourself having to rename data in your MySQL database based on a specific pattern.<\/p>\n<p>I had this very problem today. A designer had changed the dimensions of the thumbnails on a website and needed the data in the database updated to reflect the new path if they were stored in a specific directory.<\/p>\n<p>My first thought was to write a script to iterate through every row in the database, extract the data, manipulate it, then write it back. Thankfully, warning bells rang in my head and I thought about the problem again.<\/p>\n<p>MySQL offers various string manipulate routines and the <a href=\"http:\/\/dev.mysql.com\/doc\/refman\/5.0\/en\/string-functions.html#function_replace\"><code>REPLACE<\/code><\/a> takes a string, a substring to match, and a replacement to insert if a match occurs.<\/p>\n<pre class=\"lang:tsql decode:true \" title=\"Updating rows based on a text pattern\" >UPDATE artists \nSET thumbnail_url=REPLACE(thumbnail_url, '\/160x90\/','\/150x84\/') \nWHERE thumbnail_url IS NOT NULL;<\/pre>\n<p>This SQL goes through each row in the artists table where the thumbnail_url is not null, replacing each instance of \/160&#215;90\/ with \/150&#215;84\/, keeping the other data intact.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>There are times when you find yourself having to rename data in your MySQL database based on a specific pattern. I had this very problem today. A designer had changed the dimensions of the thumbnails on a website and needed the data in the database updated to reflect the new path if they were stored &hellip; <a href=\"http:\/\/www.robertprice.co.uk\/robblog\/replacing-text-in-a-mysql-database\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Replacing Text In A MySQL Database&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[2],"tags":[42],"class_list":["post-1121","post","type-post","status-publish","format-standard","hentry","category-dev","tag-mysql"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Replacing Text In A MySQL Database - Robert Price<\/title>\n<meta name=\"description\" content=\"How to rename and replace text content in a MySQL database based on a pattern.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.robertprice.co.uk\/robblog\/replacing-text-in-a-mysql-database\/\" \/>\n<meta property=\"og:locale\" content=\"en_GB\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Replacing Text In A MySQL Database - Robert Price\" \/>\n<meta property=\"og:description\" content=\"How to rename and replace text content in a MySQL database based on a pattern.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.robertprice.co.uk\/robblog\/replacing-text-in-a-mysql-database\/\" \/>\n<meta property=\"og:site_name\" content=\"Robert Price\" \/>\n<meta property=\"article:published_time\" content=\"2012-10-30T09:17:05+00:00\" \/>\n<meta name=\"author\" content=\"rob\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"rob\" \/>\n\t<meta name=\"twitter:label2\" content=\"Estimated reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.robertprice.co.uk\/robblog\/replacing-text-in-a-mysql-database\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.robertprice.co.uk\/robblog\/replacing-text-in-a-mysql-database\/\"},\"author\":{\"name\":\"rob\",\"@id\":\"http:\/\/www.robertprice.co.uk\/robblog\/#\/schema\/person\/fac6d5b076e0e14e1fb13e15b542a6c5\"},\"headline\":\"Replacing Text In A MySQL Database\",\"datePublished\":\"2012-10-30T09:17:05+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.robertprice.co.uk\/robblog\/replacing-text-in-a-mysql-database\/\"},\"wordCount\":157,\"keywords\":[\"MySQL\"],\"articleSection\":[\"Dev\"],\"inLanguage\":\"en-GB\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.robertprice.co.uk\/robblog\/replacing-text-in-a-mysql-database\/\",\"url\":\"https:\/\/www.robertprice.co.uk\/robblog\/replacing-text-in-a-mysql-database\/\",\"name\":\"Replacing Text In A MySQL Database - Robert Price\",\"isPartOf\":{\"@id\":\"http:\/\/www.robertprice.co.uk\/robblog\/#website\"},\"datePublished\":\"2012-10-30T09:17:05+00:00\",\"author\":{\"@id\":\"http:\/\/www.robertprice.co.uk\/robblog\/#\/schema\/person\/fac6d5b076e0e14e1fb13e15b542a6c5\"},\"description\":\"How to rename and replace text content in a MySQL database based on a pattern.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.robertprice.co.uk\/robblog\/replacing-text-in-a-mysql-database\/#breadcrumb\"},\"inLanguage\":\"en-GB\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.robertprice.co.uk\/robblog\/replacing-text-in-a-mysql-database\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.robertprice.co.uk\/robblog\/replacing-text-in-a-mysql-database\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"http:\/\/www.robertprice.co.uk\/robblog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Replacing Text In A MySQL Database\"}]},{\"@type\":\"WebSite\",\"@id\":\"http:\/\/www.robertprice.co.uk\/robblog\/#website\",\"url\":\"http:\/\/www.robertprice.co.uk\/robblog\/\",\"name\":\"Robert Price\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"http:\/\/www.robertprice.co.uk\/robblog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-GB\"},{\"@type\":\"Person\",\"@id\":\"http:\/\/www.robertprice.co.uk\/robblog\/#\/schema\/person\/fac6d5b076e0e14e1fb13e15b542a6c5\",\"name\":\"rob\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-GB\",\"@id\":\"http:\/\/www.robertprice.co.uk\/robblog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g\",\"caption\":\"rob\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Replacing Text In A MySQL Database - Robert Price","description":"How to rename and replace text content in a MySQL database based on a pattern.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.robertprice.co.uk\/robblog\/replacing-text-in-a-mysql-database\/","og_locale":"en_GB","og_type":"article","og_title":"Replacing Text In A MySQL Database - Robert Price","og_description":"How to rename and replace text content in a MySQL database based on a pattern.","og_url":"https:\/\/www.robertprice.co.uk\/robblog\/replacing-text-in-a-mysql-database\/","og_site_name":"Robert Price","article_published_time":"2012-10-30T09:17:05+00:00","author":"rob","twitter_card":"summary_large_image","twitter_misc":{"Written by":"rob","Estimated reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.robertprice.co.uk\/robblog\/replacing-text-in-a-mysql-database\/#article","isPartOf":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/replacing-text-in-a-mysql-database\/"},"author":{"name":"rob","@id":"http:\/\/www.robertprice.co.uk\/robblog\/#\/schema\/person\/fac6d5b076e0e14e1fb13e15b542a6c5"},"headline":"Replacing Text In A MySQL Database","datePublished":"2012-10-30T09:17:05+00:00","mainEntityOfPage":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/replacing-text-in-a-mysql-database\/"},"wordCount":157,"keywords":["MySQL"],"articleSection":["Dev"],"inLanguage":"en-GB"},{"@type":"WebPage","@id":"https:\/\/www.robertprice.co.uk\/robblog\/replacing-text-in-a-mysql-database\/","url":"https:\/\/www.robertprice.co.uk\/robblog\/replacing-text-in-a-mysql-database\/","name":"Replacing Text In A MySQL Database - Robert Price","isPartOf":{"@id":"http:\/\/www.robertprice.co.uk\/robblog\/#website"},"datePublished":"2012-10-30T09:17:05+00:00","author":{"@id":"http:\/\/www.robertprice.co.uk\/robblog\/#\/schema\/person\/fac6d5b076e0e14e1fb13e15b542a6c5"},"description":"How to rename and replace text content in a MySQL database based on a pattern.","breadcrumb":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/replacing-text-in-a-mysql-database\/#breadcrumb"},"inLanguage":"en-GB","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.robertprice.co.uk\/robblog\/replacing-text-in-a-mysql-database\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.robertprice.co.uk\/robblog\/replacing-text-in-a-mysql-database\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"http:\/\/www.robertprice.co.uk\/robblog\/"},{"@type":"ListItem","position":2,"name":"Replacing Text In A MySQL Database"}]},{"@type":"WebSite","@id":"http:\/\/www.robertprice.co.uk\/robblog\/#website","url":"http:\/\/www.robertprice.co.uk\/robblog\/","name":"Robert Price","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/www.robertprice.co.uk\/robblog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-GB"},{"@type":"Person","@id":"http:\/\/www.robertprice.co.uk\/robblog\/#\/schema\/person\/fac6d5b076e0e14e1fb13e15b542a6c5","name":"rob","image":{"@type":"ImageObject","inLanguage":"en-GB","@id":"http:\/\/www.robertprice.co.uk\/robblog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g","caption":"rob"}}]}},"_links":{"self":[{"href":"http:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/posts\/1121","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/comments?post=1121"}],"version-history":[{"count":0,"href":"http:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/posts\/1121\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/media?parent=1121"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/categories?post=1121"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/tags?post=1121"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}