0

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?

update mytb set content = trim(content)

It just removes the surrounded spaces.

Martin AJ
  • 6,261
  • 8
  • 53
  • 111

6 Answers6

0
SELECT REPLACE('content','<br>','') FROM MYTB
WHERE content LIKE '<br>%'
saravanatn
  • 630
  • 5
  • 9
0

You can use REGEXP in MYSQL:

SELECT REPLACE('content','<br>','') as content  FROM table WHERE content REGEXP '^<br>'
HamzaNig
  • 1,019
  • 1
  • 10
  • 33
0

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)]>
Bernd Buffen
  • 14,525
  • 2
  • 24
  • 39
-1
SELECT CASE WHEN substr(content,1,4) ='<br>' 
            THEN substr(content,5,len(content))  
            ELSE content 
       END as content  
Anshuman Jaiswal
  • 5,352
  • 1
  • 29
  • 46
saravanatn
  • 630
  • 5
  • 9
-1

Use MYSQL substring with if condition.

SET content = TRIM
                (IF
                  (SUBSTRING
                     (TRIM(content), 1, 4) = '<br>',
                      SUBSTRING(TRIM(content), 5), content
                  )
                );
Jithin Scaria
  • 1,271
  • 1
  • 15
  • 26
Irvin
  • 1
  • 1
-2

MYSQL has a substring function that can be used:

UPDATE mytb SET content = SUBSTRING(content, 5)
WHERE content LIKE '<br>%'
Terje D.
  • 6,250
  • 1
  • 22
  • 30