Using MAX() in VARCHAR Field

前端 未结 6 2006
清酒与你
清酒与你 2021-02-20 06:39

I have a table with following set of data

ID (VARCHAR2 field)
D001
D002
D010
D0012

I use max() in this field.

Sele         


        
相关标签:
6条回答
  • 2021-02-20 06:43

    SELECT * FROM <TABLE_NAME> ORDER BY CAST( ID AS DECIMAL( 10, 3 ) ) DESC

    0 讨论(0)
  • 2021-02-20 06:48

    below code is working for me as per your expectation

    select max(to_number(regexp_substr(id, '\d+'))) id from <yourtable>;
    
    0 讨论(0)
  • 2021-02-20 06:49

    This will surely work.

        select MAX(CAST(REPLACE(REPLACE(ID, 'D', ''), '', '') as int)) from <table-name>
    
    0 讨论(0)
  • 2021-02-20 07:01

    First Varchar need to Casted as int to select as MAX. Use below query:

    select max(CAST(ID as signed)) as max_id from <table-name>;

    0 讨论(0)
  • 2021-02-20 07:03

    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.

    0 讨论(0)
  • 2021-02-20 07:07

    this should work

    Select MAX(ID) from table where IsNumeric(ID) = 1 ; 
    
    0 讨论(0)
提交回复
热议问题