Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi to all,
Here i need to get max of value + 1. but first value is 0001 and how to get my max +1 ('0002') but i show only 2.how i get 0002.

What I have tried:

select MAx(maxid)+1 from tblcategoryuser;
it show only 2
but i need to get 0002
Posted
Updated 8-Mar-16 21:13pm
Comments
Maciej Los 9-Mar-16 1:59am    
'0001' is not numeric value. It's string representation of 1 with leading zeros.

Surely you are getting confused between the number value and its desired 'display' representation - '2' is correct from sql-server point of view

you dont say what version of SQL server you're using, 'newer' versions ?>2012 have a 'FORMAT' function FORMAT (Transact-SQL)[^] ie you might be able to do

select FORMAT(MAX(maxid)+1, 'd4') from tblcategoryuser;


If you're using < 2012, well, then you might get away with

select right('0000' + cast(MAX(maxid)+1 as varchar(4)), 4)
 
Share this answer
 
Comments
Maciej Los 9-Mar-16 1:55am    
5ed!
Animesh Datta 9-Mar-16 3:15am    
5ed!
Look like you are trying to get the next unique id by querying and incrementing the the prevailing max id. In a multi-user environment, it is not unimaginable that more than one users may get the same max id, and as a result, it will cause clashes. Instead, learn to do it correctly by reading Ask Your Database for that Unique ID[^]
 
Share this answer
 

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