Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
i used this code but convertion failed how to do ? i need increment every execution of registration and generate unicqidentity


SQL
select  'FHMS'  + max(SUBSTRING ( employeeId ,5 , 5 ))+1 as nextkey from employeemaster
Posted
Updated 21-Oct-14 0:16am
v4
Comments
VC.J 21-Oct-14 0:03am    
is employee id is string if yes than you are adding 1 into it first parse employeeid into int
Laiju k 21-Oct-14 0:21am    
SELECT MAX(ISNULL(employeeId ,0))+1

you can append 'FHMS' afterwards.
Rajesh waran 21-Oct-14 0:44am    
How u think substring(employeeId ,5 , 5) is possible for multiple entries,it will shows the same result for every repetition of execution. if the id is FHMS10001 means, the result will be 1+1=2 for every execution of this query.

Try this in C#,

SqlCommand cmd=new SqlCommand("select MAX(SUBSTRING(employeeId ,5,1000))+1 as NextKey from employeemaster",connectionstring);

SqlDataReader dr=cmd.ExecuteReader();

dr.Read();

if(dr.HasRows)
{
string str;

str="FHMS";

string id=str+dr[0].ToString();

dr.Close();
}
 
Share this answer
 
Comments
King Fisher 21-Oct-14 1:00am    
don't go for Code.refer my Solution
Rajesh waran 21-Oct-14 1:06am    
Ok sir,Thank You.
you have to Convert as nvarchar

SQL
select 'FHMS'+ cast (max(cast(SUBSTRING( employeeId ,5, 5 )as int))+1 as nvarchar) as nextkey from employeemaster
 
Share this answer
 
v2
Comments
Prasanth Radhakrishanan 21-Oct-14 6:17am    
yes sir
Do not calculate the NextId - use the built-in functionality provided by sql - i.e. use an Identity [^]column

This will auto-increment the id and more importantly will handle multi-user scenarios and race conditions far better than your code finding the previous MAX value.

The value is assigned when you create the row, and will be unique across that table.

If you want to see it in string format just use
select 'FHMS'+ cast (employeeId nvarchar) as thisKey from employeemaster

If for whatever reason (i.e. I can't think of a good enough one), you want to know what the next id is going to be before you insert it then have a look at @@IDENTITY[^]
 
Share this answer
 
Try This..
SQL
Use isnull(col,'Value') to set the value of the column if there is no data present into the table..



C#
select 'FHMS'+(Max(isnull((substring(employeeId ,5,len(employeeId ))),0))+1) as NextId from employeemaster
 
Share this answer
 
v2
SQL
DECLARE @myid uniqueidentifier
SET @myid = NEWID()
select right(@myid,12) as GetID
OR
select @myid as GetID
 
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