Click here to Skip to main content
15,879,474 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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.... :)
Posted
Comments
Estys 1-Aug-14 5:28am    
What abour a key like ACF100? Is that bigger than ACF30 in your opinion?
They are all strings, so numerical comparison does not work here.

1 solution

try this.. :)

SQL
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
 
Share this answer
 
v2
Comments
SubhashRokzzz 1-Aug-14 5:45am    
Thank You Nirav Prabtani..... Your solution works fine....Thanks a lott.... :)
Nirav Prabtani 1-Aug-14 5:50am    
Welcome subhash.. :)
Thanks7872 1-Aug-14 5:51am    
Are you sure?
Nirav Prabtani 1-Aug-14 5:55am    
I had not implemented it practically but it should work.. :)
Thanks7872 1-Aug-14 6:06am    
And thats the reason of my comment.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900