SurendraKumar Jaiswal
SurendraKumar Jaiswal

Reputation: 192

Replace template smart tags <<tag>> to [tag] in mysql

I have an table name templateType, It has column name Template_Text. The Template have many smart tags <> to [tag] using mysql, and I need to replace << to [ and >> with ].

Edit from OP's comments:

It is an template with large text and having multiple smart tags. As example : " I <<Fname>> <<Lname>>, <<UserId>> <<Designation>> of xyz organization, Proud to announce...."

Here I need to replace these << with [ and >> with ], so it will look like

" [Fname] [Lname], [UserId] ...." 

Upvotes: 2

Views: 106

Answers (2)

Mureinik
Mureinik

Reputation: 311883

A couple of replace calls should work:

SELECT REPLACE(REPLACE(template_text, '<<', '['), '>>', '])
FROM   template_type

Upvotes: 0

Madhur Bhaiya
Madhur Bhaiya

Reputation: 28854

Based on your comments, your MySQL version does not support Regex_Replace() function. So, a generic solution is not feasible.

Assuming that your string does not contain additional << and >>, other than following the <<%>> format, we can use Replace() function.

I have also added a WHERE condition, so that we pick only those rows which match our given substring criteria.

Update templateType
SET Template_Text = REPLACE(REPLACE(Template_Text, '<<', '['), '>>', ']')
WHERE Template_Text LIKE '%<<%>>%'

In case the problem is further complex, you may get some ideas from this answer: https://stackoverflow.com/a/53286571/2469308

Upvotes: 1

Related Questions