MySQL REGEXP in where not finding expected results - mysql

I have this query:
SELECT certREF,CAST(SUBSTRING_INDEX(certREF,'-',-1) as UNSIGNED) as REF
FROM certificatecatalog
WHERE certREF is not null AND `certREF` REGEXP 'F[\d]+-[\d]+'
ORDER BY REF DESC
Limit 0,1
I have data rows in column certREF like:
F17-1257
F17-3546
F18-8854
F19-9854
I want to be able to pull the highest number after "Fnumber-"
The regualar expression seems to work when i put it in one of those live regex testers.
However i'm getting an empty result set.
If someone could let me know where i'm going wrong :)
Thanks

I think the simplest way is implicit conversion:
select max(substr(certRef, 2) + 0)
from certificatecatalog
where certRef like 'F%';
MySQL will convert the digits after the first F to a number, stopping at the first non-digit. It then returns the maximum value. Regular expressions are not needed for this.
EDIT:
If you want the part after the hyphen, then you can do either:
select max(substr(certRef, 5) + 0)
from certificatecatalog
where certRef like 'F%';
or:
select max(substring_index(certRef, '-', -1) + 0)
from certificatecatalog
where certRef like 'F%';

You might use substr() with instr() function as :
select max(substr(certREF,instr(certREF,'-')+1,length(certREF)))
as maxNumber
from certificatecatalog;
MAXNUMBER
9854
SQL Fiddle Demo

Related

mysql get max number from a string field

I need to get maximum number from a part of the value that generally start with year followed by slash(/). So I need a maximum number after the slash(/) but year should be 2016
2016/422
2016/423
2016/469
2016/0470
2014/777
2015/123
2015/989
I tried this query
SELECT columname FROM tablename WHERE columname LIKE '2016/%' ORDER BY id DESC
the above query always giving '2016/469' as first record, how to get '2016/0470' as the maximum number?
any help will be much appreciated.
Thank you.
If columname follows that pattern YEAR/0000, you can use SUBSTRING function from MySQL to remove the part of the string you don't want.
SELECT value FROM (
SELECT CAST(SUBSTRING(columname, 0, 4) AS UNSIGNED) as year, CAST(SUBSTRING(columname FROM 6) AS UNSIGNED) as value FROM tablename
) total
ORDER BY year DESC, value DESC
LIMIT 1;
You need to split the string into 2 parts and evaluate them as numbers, instead of strings. The following formula will return the number after the / in the fieldname. All functions used below are described in the string functions section of the MySQL documentation. This way you can get the number after the / character, even if it is not year before the /, but sg else. The + 0 converts the string to a number, eliminating any leading 0.
select right(columnname, char_length(columnname)-locate('/',columnname)) + 0
from tablename
Just take the max() of the above expression to get the expected results.
UPDATE:
If you need the original number and the result has to be restricted to a specific year, then you need to join back the results to the original table:
select columnname
from tablename t1
inner join (select max(right(t.columnname, char_length(t.columnname)-locate('/',t.columnname)) + 0) as max_num
from tablename t
where left(t.columnname,4)='2016'
) t2
on right(t1.columnname, char_length(1t.columnname)-locate('/',t1.columnname)) + 0 = t2.max_num
where left(t1.columnname,4)='2016'
There are lots of suggestions given as answers already. But some of those seem overkill to me.
Seems like the only change needed to the OP query is the expression in the ORDER BY clause.
Instead of:
ORDER BY id
We just need to order by the numeric value following the slash. And there are several approaches, several expressions, that will get that from the example data.
Since the query already includes a condition columname LIKE '2016/%'
We can get the characters after the first five characters, and then convert that string to a numeric value by adding zero.
ORDER BY SUBSTRING(columname,6) + 0 DESC
If we only want to return one row, add
LIMIT 1
http://dev.mysql.com/doc/refman/5.7/en/string-functions.html#function_substring
If we only want to return the numeric value, we could use the same expression in the SELECT list, in addition columnname.
This isn't the only approach. There are lots of other approaches that will work, and don't use SUBSTRING.
Try like this:
SELECT
MAX(CAST(SUBSTRING(t.name,
LOCATE('/', t.name) + 1)
AS UNSIGNED)) AS max_value
FROM
tablename AS t;
You can try with this little uggly approach:
SELECT t.id, t2.secondNumber FROM table AS t
JOIN (SELECT id,
CONCAT(SUBSTRING(field,1,5),
if(SUBSTRING(SUBSTRING(field, 6),1,1)='0',
SUBSTRING(field, 6),
SUBSTRING(field,7)
)
) as secondNumber FROM table ) AS t2 ON t2.id=t.id
ORDER BY t2.secondNumber DESC
Would be valid only if the 0 (zeroes) before the second number (after the slash) are no more than 1.
Or if the year doesn`t matter you can try to order them only by the second number if it is ok:
SELECT t.id, t2.secondNumber FROM table AS t
JOIN (SELECT id,
if(SUBSTRING(SUBSTRING(field, 6),1,1)='0',
SUBSTRING(field, 6),
SUBSTRING(field,7)
) as secondNumber FROM table ) AS t2 ON t2.id=t.id
ORDER BY t2.secondNumber DESC

how to check value is numeric or not a numeric in mysql select query as follow

value->{1,2,Yes,No,5,6}
select if((value is numeric),value,'not a numeric') as column_name
how to implement this if in my mysql select query
This should do it :)
select if(field REGEXP '^-?[0-9]+$' > 0, field, 'not a numeric') as column_name
Example:
SELECT '12345' REGEXP '^-?[0-9]+$'
Returns: 1 (its a number)
SELECT 'abcdef' REGEXP '^-?[0-9]+$'
Returns: 0 (its NOT a number)
This is an old question, but when I needed the same thing the fastest solution was
select if((value *1),value, 'not a number') as column_name;
Granted, this will not consider zero a number, but neither did the Romans and they did ok for thousands of years. For me, given the improved speed across the hundreds of millions of rows, this was the best solution.
You can try it-
SELECT IF(table_column*1>0,table_column,'Not a Numeric')
FROM your_table;
Note: If there is a value "9 hello" then it will be treat as numeric as 9 is numeric value, if you need to exclude it also then revert. But in your question there is no such value.

mysql alphabetical order applied to a string

Is it a way in mysql to alphabetically ordering a string ?
I am looking for a function who does that :
select alphabeticallyorder('cba')
will return me
'abc'
A query like this should return the values that you need. I know it's not a nice query, and you also need a numbers table, filled with numbers:
SELECT col, GROUP_CONCAT(SUBSTRING(col, n, 1)
ORDER BY SUBSTRING(col, n, 1)
SEPARATOR '') AS ordered_col
FROM
tablename INNER JOIN numbers
ON LENGTH(tablename.col)>=numbers.n
GROUP BY
id, col
Also it will work only if LENGTH(col)=CHAR_LENGTH(col). Please see fiddle here.
Please see the REVERSE function.
http://dev.mysql.com/doc/refman/5.5/en/string-functions.html#function_reverse
mysql> SELECT REVERSE('abc');
-> 'cba'

MySQL LIKE with range doesn't work

I've got a database table mytable with a column name in Varchar format, and column date with Datetime values. I'd like to count names with certain parameters grouped by date. Here is what I do:
SELECT
CAST(t.date AS DATE) AS 'date',
COUNT(*) AS total,
SUM(LENGTH(LTRIM(RTRIM(t.name))) > 4
AND (LOWER(t.name) LIKE '%[a-z]%')) AS 'n'
FROM
mytable t
GROUP BY
CAST(t.date AS DATE)
It seems that there's something wrong with range syntax here, if I just do LIKE 'a%' it does count properly all the fields starting with 'a'. However, the query above returns 0 for n, although should count all the fields containing at least one letter.
You write:
It seems that there's something wrong with range syntax here
Indeed so. MySQL's LIKE operator (and SQL generally) does not support range notation, merely simple wildcards.
Try MySQL's nonstandard RLIKE (a.k.a. REGEXP), for fuller-featured pattern matching.
I believe LIKE is just for searching for parts of a string, but it sounds like you want to implement a regular expression to search for a range.
In that case, use REGEXP instead. For example (simplified):
SELECT * FROM mytable WHERE name REGEXP "[a-z]"
Your current query is looking for a string of literally "[a-z]".
Updated:
SELECT
CAST(t.date AS DATE) AS 'date',
COUNT(*) AS total,
SUM(LENGTH(LTRIM(RTRIM(t.name))) > 4
AND (LOWER(t.name) REGEXP '%[a-z]%')) AS 'n'
FROM
mytable t
GROUP BY
CAST(t.date AS DATE)
I believe you want to use WHERE REGEXP '^[a-z]$' instead of LIKE.
You have regex in your LIKE statement, which doesn't work. You need to use RLIKE or REGEXP.
SELECT CAST(t.date AS DATE) AS date,
COUNT(*) AS total
FROM mytable AS t
WHERE t.name REGEXP '%[a-zA-Z]%'
GROUP BY CAST(t.date AS DATE)
HAVING SUM(LENGTH(LTRIM(RTRIM(t.name))) > 4
Also just FYI, MySQL is terrible with strings, so you really should trim before you insert into the database. That way you don't get all that crazy overhead everytime you want to select.

MySQL sort varchar column numeric, numbers first

I've got a varchar column that I want to sort numeric, which works great when using this trick: https://stackoverflow.com/a/5418033/1005334 (in short: ...ORDER BY Result * 1).
However, the table concerned contains results. So something like this occurs:
Result
------
DNS
DNF
1
2
3
The numbers are correctly ordered, but the DNF comes above the numbers when sorting like this. What I'd like is to have the numeric sort, but with non-numbers sorted alphabetically below the numbers. Like so:
Result
------
1
2
3
DNF
DNS
In what way can I modify the query (preferably only the ORDER BY clause) to get this result?
use LPAD
http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_lpad
LPAD(yourField, 20, '0');
this will manage correct order for "varchar numeric fields" (10 will be after 2) and put strings at the end.
SqlFiddle
The second argument (20) is quite arbitrary. It should be equivalent to (or bigger then) the length of the longest string in your field.
SELECT *, (Result REGEXP '^[0-9]+$') AS is_numeric
FROM table_name
ORDER BY is_numeric DESC,
CASE WHEN is_numeric THEN (Result + 0) ELSE Result END ASC
You can do that by using MySQL's REGEXP. Try this one,
SELECT *
FROM tablea
ORDER BY IF(`Result` REGEXP '^-?[0-9]+$', 0, 1) ASC,
`Result` ASC
SQLFiddle Demo
try this:
Please change your ORDER BY Clause with this:
ORDER BY
CASE WHEN Result REGEXP '^[0-9]+$' THEN Result*1 else 999999 END,
Result
This will order the numeric values first then the rest
ORDER BY CAST(`Result` AS SIGNED) DESC
this should work.