+32 472 40 86 79
thijs@feryn.eu
Thijs Feryn
  • Home
  • About me
  • Speaking
  • Books
  • Blog
  • Vlog
  • Home
  • About me
  • Speaking
  • Books
  • Blog
  • Vlog
  • Home
  • Tech
  • Find and replace by last occurrence in MySQL

Tech

09 Jun

Find and replace by last occurrence in MySQL

Find and replace in MySQL based on the last occurrence

Doing find and replace tasks are very common in IT. Usually we trying to find a substring and replace it with something else. Typical needle and haystack kind of stuff. But the thing is that the “find” part is based on the first occurrence of the needle in the haystack.

In this blog post, I want to show you how to do a typical find and replace in MySQL based on the last occurrence of a string.

Last occurrence find and replace

The dummy query above looks for the term “feryn.eu” in the string “feryn.eu.bla.feryn.eu” and replaces the last occurrence of “feryn.eu” with “thijs.be”. The end result is the string “feryn.eu.bla.thijs.be”.

This query doesn’t really return the position, but rather the data that comes before the last occurrence of our so-called needle.

I didn’t figure this out myself, but found it on Stackoverflow.

Use it in an update statement

Want to start using the find and replace trick in an update statement? Here’s an example:

In the example above, I’m replacing the last occurrence of “feryn.eu” by “thijs.be” in the “someField” field.

Tags:findlast occurencemysqlreplacesqlsubstring
  • Share:

Book me for your next event

Get in touch

Follow me on social media

Buy my book

Getting Started with Varnish Cache
More presentations

Categories

  • Combell
  • Football
  • Interviews
  • Review
  • Speaking
  • Tech
  • Travel
  • Trip report
  • Uncategorized
  • Video
  • Vlog

Recent Posts

Logitech MX Vertical mouse
Logitech MX Vertical mouse product review
14Jan,2019
Airtame product review - VLOG 21
Airtame product review
09Oct,2018
PHPBenelux Conference 2018 behind the scenes - VLOG 10
PHPBenelux Conference 2018 behind the scenes – VLOG 10
31Jan,2018

Get in touch

+32 472408679

thijs@feryn.eu

Follow me on social media

  • Twitter
  • Linkedin
  • Instagram
  • Youtube

Navigation

  • Home
  • About me
  • Speaking
  • Speaker bio
  • Books
  • Blog
  • Video
  • Vlog