patternsqlMinor
Removing Brackets and the content
Viewed 0 times
theremovingbracketsandcontent
Problem
I have a column in my database called Horse_Name. This currently includes the country if outside of UK in brackets (ire),(fra) ect.
Is it possible to run a query to remove the brackets and the content very much like in excel whereby you can replace all (*) within the given column?
An example would be as follows
I would like to go through the whole column named Horse_Name to remove the brackets and data.
I have tried the following code but it only finds the values and does not replace them
Kind Regards.
Is it possible to run a query to remove the brackets and the content very much like in excel whereby you can replace all (*) within the given column?
An example would be as follows
Current Outcome
----------------+--------------
Horse_Name | Horse_Name
----------------+--------------
Rocket(FRA) | Rocket
Apple Jade(IRE) | Apple Jade
Zac | ZacI would like to go through the whole column named Horse_Name to remove the brackets and data.
I have tried the following code but it only finds the values and does not replace them
SELECT REPLACE(Horse_Name,
SUBSTRING(Horse_Name, LOCATE('(', Horse_Name), LENGTH(Horse_Name) - LOCATE(')',
REVERSE(Horse_Name)) - LOCATE('(', Horse_Name) + 2), '') AS Horse_Name
FROM TFResults;Kind Regards.
Solution
I've set up this example:
If last parenthese is located always at end of the string you could use next syntax:
But if there are additional characters after the last parenthese you could CONCAT left and right substrings in this way:
This is the final result (note I've added a
| horse_name |
| :----------- |
| Rocket |
| Apple Jade 2 |
| Zac |
db<>fiddle here
create table tbl (horse_name varchar(100));
insert into tbl values ('Rocket(FRA)'),('Apple Jade(IRE) 2'),('Zac');If last parenthese is located always at end of the string you could use next syntax:
select case when locate('(', horse_name) = 0
then horse_name
else left(horse_name, locate('(', horse_name) - 1)
end as horse_name
from tbl;But if there are additional characters after the last parenthese you could CONCAT left and right substrings in this way:
select case when locate('(', horse_name) = 0
then horse_name
else concat(left(horse_name, locate('(', horse_name) - 1),
right(horse_name, length(horse_name) - locate(')', horse_name)))
end as horse_name
from tbl;This is the final result (note I've added a
2 after (IRE):| horse_name |
| :----------- |
| Rocket |
| Apple Jade 2 |
| Zac |
db<>fiddle here
Code Snippets
create table tbl (horse_name varchar(100));
insert into tbl values ('Rocket(FRA)'),('Apple Jade(IRE) 2'),('Zac');select case when locate('(', horse_name) = 0
then horse_name
else left(horse_name, locate('(', horse_name) - 1)
end as horse_name
from tbl;select case when locate('(', horse_name) = 0
then horse_name
else concat(left(horse_name, locate('(', horse_name) - 1),
right(horse_name, length(horse_name) - locate(')', horse_name)))
end as horse_name
from tbl;Context
StackExchange Database Administrators Q#206914, answer score: 2
Revisions (0)
No revisions yet.