我有一个包含以下数据集的表格
ID (VARCHAR2 field)
D001
D002
D010
D0012
我在这个字段中使用max()
。
Select max(ID) from <table-name>;
它返回结果为D010
。
为什么结果不是D0012
?
你收到 D010
是因为按字母顺序,D010
在 D0012
之后,或者说,D01
在 D00
之后,因此任何以 D01x
开头的内容都在以 D00x
开头的内容之后。
select max(to_number(regexp_substr(id, '\d+'))) id from <yourtable>;
SELECT *
FROM `<TABLE_NAME>`
ORDER BY CAST( `ID` AS DECIMAL( 10, 3 ) ) DESC
这应该可以工作
Select MAX(ID) from table where IsNumeric(ID) = 1 ;
需要将第一个Varchar强制转换为int才能选择MAX。使用以下查询:
select max(CAST(ID as signed)) as max_id from <table-name>;
这肯定会起作用。
select MAX(CAST(REPLACE(REPLACE(ID, 'D', ''), '', '') as int)) from <table-name>
varchar
字段,为什么要按数字排序? - Kirk Woll