如何从sql server中的Id字段获取最大ID [英] How do I get Maximum ID from an Id field in sql server
问题描述
嗨朋友
请问任何人请告诉我如何从sqlserver表中获取最大id。
我在sql中有一个表带有ID字段的服务器(主键)。其中包含的数据如
-------------
ID
---------------
ACF8
ACF10
ACF6
ACF13
ACF15
ACF9
ACF30
当我尝试从这一列获取最大ID时,我得到ACF9作为最大ID而不是ACF30的
。
我也尝试过以下查询。
从TableFacility中选择isnull(convert(int,SUBSTRING(MAX(ID),4,1)),0) 。
如果有人对此有所了解,请帮助我。
谢谢.....
欢乐周结束所有....:)
Hi friends
Could any one please tell me how to get maximum id from sqlserver table.
I have a table in sql server with an ID field(primary key).Which contains data like
-------------
ID
---------------
ACF8
ACF10
ACF6
ACF13
ACF15
ACF9
ACF30
when ever I try to take the maximum id from this column I am getting ACF9 as maximum id instead
of ACF30.
I have tried with the below query also.
select isnull(convert(int,SUBSTRING(MAX(ID),4,1)),0) from TableFacility .
Please help me if anyone has idea about this.
Thank You.....
Happy Week end to all.... :)
推荐答案
试试这个.. :)
try this.. :)
with samp as(
select isnull(convert(int,SUBSTRING(ID,4,1)),0)as ID from TableFacility
)
SELECT 'ACF'+convert(varchar,MAX(ID)) as MaxID FROM samp
这篇关于如何从sql server中的Id字段获取最大ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!