Tuesday, 26 June 2012

Generate Employee Id in Sql Server

--exec Sp_empid
alter procedure Sp_empid
--EMP000001
as
declare @no int
declare @emid nvarchar(50)
declare @nolen int
set @no=(select CONVERT(INT,SUBSTRING(max(EmpID),4,9)+1) from EMPLOYEE)
set @nolen=LEN(@no)
if(@nolen=1)
begin
    set @emid='EMP00000'+CONVERT(nvarchar,@no)
end
else if(@nolen=2)
begin
    set @emid='EMP0000'+CONVERT(nvarchar,@no)
end
else if(@nolen=3)
begin
    set @emid='EMP000'+CONVERT(nvarchar,@no)
end
else if(@nolen=4)
begin
    set @emid='EMP00'+CONVERT(nvarchar,@no)
end
else if(@nolen=5)
begin
    set @emid='EMP0'+CONVERT(nvarchar,@no)
end
else if(@nolen=6)
begin
    set @emid='EMP'+CONVERT(nvarchar,@no)
end
else
begin
    set @emid='EMP000001'
end
select @emid as emidno

No comments:

Post a Comment