how to select max of mixed string/int column?

47,942

Solution 1

HKL9 (string) is greater than HKL15, because they are compared as strings. One way to deal with your problem is to define a column function that returns only the numeric part of the invoice number.

If all your invoice numbers start with HKL, then you can use:

SELECT MAX(CAST(SUBSTRING(invoice_number, 4, length(invoice_number)-3) AS UNSIGNED)) FROM table

It takes the invoice_number excluding the 3 first characters, converts to int, and selects max from it.

Solution 2

select ifnull(max(CONVERT(invoice_number, SIGNED INTEGER)), 0)
from invoice_header 
where invoice_number REGEXP '^[0-9]+$'

Solution 3

After a while of searching I found the easiest solution to this.

select MAX(CAST(REPLACE(REPLACE(invoice_number , 'HKL', ''), '', '') as int)) from invoice_header

Solution 4

This should work also

SELECT invoice_number
FROM invoice_header
ORDER BY LENGTH( invoice_number) DESC,invoice_number DESC 
LIMIT 0,1

Solution 5

Your problem is more one of definition & design.

Select the invoice number with highest ID or DATE, or -- if those really don't correlate with "highest invoice number" -- define an additional column, which does correlate with invoice-number and is simple enough for the poor database to understand.

select INVOICE_NUMBER 
from INVOICE_HEADER
order by ID desc limit 1;

It's not that the database isn't smart enough.. it's that you're asking it the wrong question.

Share:
47,942
CairoCoder
Author by

CairoCoder

I majored in Bioinformatics but have a passion for User Experience and Web Development. Everything I know about UI, UE, CSS, HTML, jQuery, PHP and MySQL was all self taught by looking online and buying books and video tutorials related to UX and Web Development. I have been continuously employed since high school and throughout college, which helped ramp me to my current positions at RAViN Jeanswear. I'm also in love with my current position at RAViN Jeanswear as Senior Web Developer and Team Leader. I am discovering that I seriously enjoy user flow, User Experience and Web Development, and would like to transition my career towards this wire-framing and development path. I love figuring out what makes users tick and click, also I adore developing easy to use and smooth back-ends. I also am getting serious about my Web Design, so feel free to message me if you would like a web design, psd to html/css or anything else! I would currently like to learn (or get to know better): User Experience Research, SASS, HTML 5, jQuery/jQuery-UI, JavaScript, AJAX, IOS Development. ------- Interesting Points of Discussion ------- 1. What's more important? Creating a product to create fantastic metrics? Or creating a product that creates a "good feeling" and a lasting impression? 2. When should you use tables, and when should you use CSS? Always CSS for presentation, of course! But sometimes divititis will get you down. How do you get around that? 3. Find me a way to wrap a really long word (like supercalifragilisticexpialidocious) onto the next line of a 20px width div in CSS that works in all browsers. (?) 4. When to use CMS, and when to start a "From Scratch" website? Specialties: HTML Markup, CSS, User Experience, User Interface, Photoshop, Photography, Agile, JIRA, PHP, OOP, JavaScript, jQuery, PHP Frameworks.

Updated on July 09, 2022

Comments

  • CairoCoder
    CairoCoder almost 2 years

    Lets say that I have a table which contains a column for invoice number, the data type is VARCHAR with mixed string/int values like:

    invoice_number
    **************
        HKL1
        HKL2
        HKL3
        .....
        HKL12
        HKL13
        HKL14
        HKL15
    

    I tried to select max of it, but it returns with "HKL9", not the highest value "HKL15".

    SELECT MAX( invoice_number )
    FROM `invoice_header`
    
  • CairoCoder
    CairoCoder almost 11 years
    Thanks @Thomas W, but how I should ask for it, if I am asking the wrong way?
  • CairoCoder
    CairoCoder almost 11 years
    I think also it's order by desc.
  • CairoCoder
    CairoCoder almost 11 years
    Thank @nakosspy, but how I can do that using php/mysql?
  • Thomas W
    Thomas W almost 11 years
    The highest-numbered Invoice should be the most-recent; by ID, or by Date. You should't expect databases to decode arbitrary strings, but they're really fast at doing primary-key index scans :)
  • nakosspy
    nakosspy almost 11 years
    The statement is standard SQL. You can execute it as any other statement.
  • CairoCoder
    CairoCoder almost 11 years
    I got it, it's: SELECT max(substring(invoice_number, 4, length(invoice_number)-3)) FROM table
  • CairoCoder
    CairoCoder almost 11 years
    SELECT MAX(CAST(SUBSTRING(invoice_number, 4, length(invoice_number)-3) AS UNSIGNED)) FROM table
  • Vincent
    Vincent over 7 years
    This is the only correct answer here because it works even if the invoice numbers have varying formats and it works without modifying the database.
  • David Buck
    David Buck over 4 years
    When answering an old question, your answer would be much more useful to other StackOverflow users if you included some context to explain how your answer helps. See: How do I write a good answer.
  • DarkSide
    DarkSide almost 3 years
    This doesn't extract numeric part of invoice_number in case there are leading letters