I have a table with following set of data
ID (VARCHAR2 field)
D001
D002
D010
D0012
I use max() in this field.
Select max(ID) from <table-name>;
It returns D010 as result.
Why is the result not D0012?
The MAX() function can be allpied on the varchar columns.
When VARCHAR(MAX) exceeds 8,000 characters, the pointer is stored “in row”, and the string is stored in “LOB” pages.
MAX can be used with numeric, character, and datetime columns, but not with bit columns.
Values in VARCHAR columns are variable-length strings. The length can be specified as a value from 0 to 65,535. The effective maximum length of a VARCHAR is subject to the maximum row size (65,535 bytes, which is shared among all columns) and the character set used.
You get D010 because alphabetically, D010 comes after D0012 or said another way, D01 comes after D00 and therefore anything that is D01x comes after anything that starts D00x.
below code is working for me as per your expectation
select max(to_number(regexp_substr(id, '\d+'))) id from <yourtable>;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With