Question

I have a column invoice_id in table invoices. The invoice_id format is

INV-2-43-001-01
INV-2-43-002-01
INV-2-43-002-02

And i want to get the highest value in bold columns.

Was it helpful?

Solution

If this is the format you can get it by two ways,I have used SUBSTRING_INDEX() to get the number and one approach is use the limit with order by

select 
SUBSTRING_INDEX(SUBSTRING_INDEX(Inv,'-',-2),'-',1) inv
FROM Table1
ORDER BY inv DESC
LIMIT 1;

And other is to use the MAX() function

select 
MAX(SUBSTRING_INDEX(SUBSTRING_INDEX(Inv,'-',-2),'-',1)) inv
FROM Table1

Fiddle Demo

OTHER TIPS

You can do it also in this way:

SELECT
REVERSE(SUBSTRING(REVERSE(Inv),4, 3))
FROM Table1

SQLFiddle Demo

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top