Forum Moderators: open
If (97) is actually part of what's stored in the field, you'd do a regular expression after extracting it, like
$field =~ s/\s*\(\d+\)\s*$//; # perl
$field = preg_replace("/\s*\(\d+\)\s*$",'',$field); # php
But it may have to do with the way you're selecting, normally
select [comma-separated field list] from table;
. . and when I do a query (in this case, to put into a graph) ... I don't need the number.
I think there might be a select to extract the data without the (number,) but overall it might be simpler to get the data and alter it after extraction.
To select, there are a number of ways. You'd have to experiment with what's fastest and easiest to automate in your code:
select field from table where field like 'ThisIsMyExample%';
Combining like and % is like a wild card, "Starts with 'ThisIsMyExample' and anything that follows it." Putting % at the front is the same concept.
A regular expression might be slower but would be more specific:
select field from table where field regexp '^ThisIsMyExample[[:blank:]]*\([[:digit:]]+\)[[:blank:]]*$';
which means
- ^ starts with
- followed by ThisIsMyExample
- [[:blank:]]* followed by zero or more spaces
- followed by ( (escaped with (, which is a special character in regexps)
- [[:digit:]]+ followed by one or more digits
followed by )
- [[:blank:]]* followed by zero or more spaces
- $ end of pattern (nothing after this)
Although, there are different approaches to case-sensitivity when doing a regexp in mysql, see the documentation [dev.mysql.com] for the gory details.
SELECT SUBSTRING_INDEX(YOUR_FIELD, '(',1);
e.g. using a text example
SELECT SUBSTRING_INDEX('Thisismyexample (97)', '(',1);
returns
Thisismyexample
If you want to do it in php code
$field = $row['YOUR_FIELD'];
if(strpos($field, '(')) {
$field = substr($field, 0, strpos($field, '('));
}