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
but when query for
select max(invoice_number) maxinv invoice
it returns me "9". why that?
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
Post a Comment