简体   繁体   中英

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?

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>')
;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM