开发者

Replacing the first <br /> tag in MySQL column

开发者 https://www.devze.com 2023-02-09 01:47 出处:网络
There is some text column containing some value. E.g. 开发者_如何学JAVA<ul> <li>bla bla&nbsp;bla bla bla&nbsp;bla bla bla.</li>

There is some text column containing some value. E.g.

开发者_如何学JAVA<ul>
<li>bla bla&nbsp;bla bla bla&nbsp;bla bla bla.</li>
<li>bla bla&nbsp;bla bla bla&nbsp;bla bla bla.</li>
</ul>

or

<br />• text text text.
<br />• text text text.
<br />

How do I remove only the first <br/> tag in each record that begins with it?


If you only want to remove the 1st <br /> if the record starts with <br />:

UPDATE table
SET field = SUBSTRING(field, 7)
WHERE LEFT(field, 6) = '<br />'


You can just use the mysql trim function:

TRIM(LEADING '<br />' FROM column_name)

example:

SELECT TRIM(LEADING '<br />' FROM doyouknow) as doyouknow FROM actors;

This will remove the "<br />" from the start of doyouknow if it is there or do nothing. This will not change the data in your database like an update query.


SELECT
  CASE WHEN doyouknow LIKE '<br />%' THEN SUBSTRING(doyouknow, 7) ELSE doyouknow END,
  othercol1,
  othercol
FROM..

If you need to update the data in the table, use this

UPDATE TBL
SET doyouknow =
  CASE WHEN doyouknow LIKE '<br />%' THEN SUBSTRING(doyouknow, 7) ELSE doyouknow END,
0

精彩评论

暂无评论...
验证码 换一张
取 消