Wednesday, February 9, 2022

[FIXED] Replacing SQL field content from pattern till the end of line

Issue

I am struggling with a problem in my database. Basically, I have lots of data with various content but luckily I have always added "Kind Regards" keywords in posts and after that I have added various social media links. The links are sometimes the same and sometimes different. But the keyword "Kind Regards" is always there. The structure is like this

Kind Regards 
Linkedin Facebook Google Image link 
Some Random Text 

I want to replace everything after Kind Regards including Kind Regards with just Kind Regards R

i.e. I want to replace "Kind Regards blah_links blah_link blah_links" with just "Kind Regards R"

I tried following query in SQL tab of phpmyadmin but it didin't affect anything

    UPDATE wl_content SET message = REPLACE(message,'Kind Regards%','Kind Regards R') 

I hope anyone can guide me on how to do this replace.


Solution

UPDATE wl_content 
SET message = CONCAT(SUBSTRING_INDEX(message,
                                     'Kind Regards', 
                                     1), 
                     'Kind Regards R')

fiddle



Answered By - Akina

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.