mysql - MAX(Column) returns me a wrong value -


i want retrieve maximum invoice number invoice table returns following when select records can see maximum invoice no "10".

select * invoice 

enter image description here

but when query for

select max(invoice_number) maxinv invoice 

it returns me "9". why that?

enter image description here

this situation can occur if invoice_number stored text column e.g. varchar(10). in case, based on alphabetical order, 9 maximum value.

ideally, should storing values on want perform numerical operations numeric datatypes e.g. int. however, if reason cannot change column datatype, can try casting column before applying max, so:

select max (convert(invoice_number, signed integer)) maxinv invoice 

note: mention "values on want perform numerical operations" because there cases input text entirely numeric, such phone numbers or perhaps credit card numbers, there no scenario in want add 2 phone numbers, or take square root of credit card number. such values should stored text.


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -