34,576
社区成员
发帖
与我相关
我的任务
分享
(id,patindex('%[0-9]%',id),len(id)) as int
select 'A1' id,'aaa' name into #tb
union all select 'A2','bbb'
union all select 'A3','ccc'
union all select 'A4','ddd'
union all select 'A5','eee'
select MAX(id) max_id from #tb
/*
max_id
------
A5
(1 row(s) affected)
*/
--楼上的是按字符串来排序的,
--> 测试数据: [tb]
if object_id('[tb]') is not null drop table [tb]
create table [tb] (id varchar(20),name varchar(3))
insert into [tb]
select 'A1','aaa' union all
select 'A2','bbb' union all
select 'A3','ccc' union all
select 'A4','ddd' union all
select 'A5','eee' union all
select 'A11','eee'
select top 1 id from [tb] order by cast(substring(id,patindex('%[0-9]%',id),len(id)) as int) desc
--结果:
A11
select top 1 * from tb order by id desc
select MAX(id) from tb
select MAX(id) from tb