0

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

1
  • Use REGEXP '^F[0-9]+-[0-9]+$' (see demo). Commented Sep 26, 2018 at 15:47

2 Answers 2

1

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%';
Sign up to request clarification or add additional context in comments.

1 Comment

Hi, Sorry i perhaps needed to expand on the data in the table more, there are some with FE4-99, FR15-55, it's just the F on it's own i need.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.