Edward Sheriff Curtis
Edward Sheriff Curtis

Reputation: 497

Using replace and concat in MySQL 8 version

I have strings values such as the following in a column:

<span style=text-align: justify;>Lorem Ipsum is simply dummy text of the printing and typesetting industry.</span>

I want to perform a balanced string replacement as follows:

<p style=margin-top: 0;>Lorem Ipsum is simply dummy text of the printing and typesetting industry.</p>

I have tried to do this with the REPLACE and CONCAT functions but didn't get the desired output.

For example:

mysql> SELECT REPLACE (
        contents,
        CONCAT( "<span style=text-align: justify;>", contents, "</span>" ),
CONCAT( "<p style=margin-top: 0;>", contents, "</p>" )) q 
FROM
    t_contents 
WHERE
    contents LIKE '%<span style=text-align: justify;>%';

+--------------------------------------------------------------------------------------------------------------------+
| q                                                                                                                  |
+--------------------------------------------------------------------------------------------------------------------+
| <span style=text-align: justify;>Lorem Ipsum is simply dummy text of the printing and typesetting industry.</span> |
+--------------------------------------------------------------------------------------------------------------------+
1 row in set (0.14 sec)

How can I achieve this result in MySQL 8 version?

Upvotes: 0

Views: 150

Answers (1)

Bernd Buffen
Bernd Buffen

Reputation: 15057

try something like this:

SELECT REPLACE( REPLACE(
'<span style=text-align: justify;>Lorem Ipsum is simply dummy text of the printing and typesetting industry.</span>','<span style=text-align: justify;>','<p style=margin-top: 0;>'),
'</span>','</p>')
;

Upvotes: 1

Related Questions