How to remove s that are in the beginning of the string?

Multi tool use
Multi tool use


How to remove <br>s that are in the beginning of the string?



Here is my table:


-- mytb
+----+---------------------------+
| id | content |
+----+---------------------------+
| 1 | <br> whatever .. |
| 2 | whatever <br> whatever .. |
| 3 | <br> whatever .. |
| 4 | <br> whatever <br> .. |
+----+---------------------------+



I need to remove all <br>s that are in the beginning of the string. I can specify it using ^ in PHP regex. How can I do the same in MySQL?


<br>


^


update mytb set content = trim(content)



It just removes the surrounded spaces.





wich version of mysql are you using ?
– Daniel E.
Jul 2 at 8:44





set content = REPLACE(content, '<br>', '') ?
– Fred
Jul 2 at 8:46



set content = REPLACE(content, '<br>', '')





Do you want to remove <br> only at the start of the string or all <br>'s in the string?
– Fred
Jul 2 at 8:58


<br>


<br>




6 Answers
6



You can use a query like this:


SELECT REGEXP_REPLACE('<br>Hello<br>world<br>','^(<br>)*(.*?)(<br>)*$','\2');



Sample


MariaDB [(none)]> SELECT REGEXP_REPLACE('<br>Hello<br>world<br>','^(<br>)*(.*?)(<br>)*$','\2');
+------------------------------------------------------------------------+
| REGEXP_REPLACE('<br>Hello<br>world<br>','^(<br>)*(.*?)(<br>)*$','\2') |
+------------------------------------------------------------------------+
| Hello<br>world |
+------------------------------------------------------------------------+
1 row in set (0.00 sec)

MariaDB [(none)]> SELECT REGEXP_REPLACE('Hello<br>world<br>','^(<br>)*(.*?)(<br>)*$','\2');
+--------------------------------------------------------------------+
| REGEXP_REPLACE('Hello<br>world<br>','^(<br>)*(.*?)(<br>)*$','\2') |
+--------------------------------------------------------------------+
| Hello<br>world |
+--------------------------------------------------------------------+
1 row in set (0.00 sec)

MariaDB [(none)]> SELECT REGEXP_REPLACE('<br>Hello<br>world','^(<br>)*(.*?)(<br>)*$','\2');
+--------------------------------------------------------------------+
| REGEXP_REPLACE('<br>Hello<br>world','^(<br>)*(.*?)(<br>)*$','\2') |
+--------------------------------------------------------------------+
| Hello<br>world |
+--------------------------------------------------------------------+
1 row in set (0.00 sec)

MariaDB [(none)]> SELECT REGEXP_REPLACE('Hello<br>world','^(<br>)*(.*?)(<br>)*$','\2');
+----------------------------------------------------------------+
| REGEXP_REPLACE('Hello<br>world','^(<br>)*(.*?)(<br>)*$','\2') |
+----------------------------------------------------------------+
| Hello<br>world |
+----------------------------------------------------------------+
1 row in set (0.00 sec)

MariaDB [(none)]>





Mariadb here and he asks for mysql.
– Daniel E.
Jul 2 at 14:42



You can use REGEXP in MYSQL:


SELECT REPLACE('content','<br>','') as content FROM table WHERE content REGEXP '^<br>'


SELECT REPLACE('content','<br>','') FROM MYTB
WHERE content LIKE '<br>%'





He said starting <br> not all <br>
– Daniel E.
Jul 2 at 8:48





Thanks for pointing it out
– saravanatn
Jul 2 at 8:53





replace() will replace all occurences in the string. I.e. it will replace both '<br>' from the record with id=4
– Terje D.
Jul 2 at 9:31


replace()


SELECT CASE WHEN substr(content,1,4) ='<br>'
THEN substr(content,5,len(content))
ELSE content
END as content



Use MYSQL substring with if condition.


SET content = TRIM
(IF
(SUBSTRING
(TRIM(content), 1, 4) = '<br>',
SUBSTRING(TRIM(content), 5), content
)
);



MYSQL has a substring function that can be used:


substring


UPDATE mytb SET content = SUBSTRING(content, 5)
WHERE content LIKE '<br>%'






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

gie4zYIHmudSljs7S
MKnLBApFf260 cytpb3S5,WGTTQqygyX,xN VqMOZhl4PJ7BKCbL0Em2VLgcvds q3W5w0AYyX4vkfHp57 9zWEriG4As

Popular posts from this blog

Rothschild family

Cinema of Italy