What you want to see on DotNetFunda.com ?
DotNetFunda.Com Logo
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 35471 |  Welcome, Guest!   Register  Login
 Home > Code Snippets > SQL Server > Generating employee code like EMP007 in SQLSERVER2008 ...
CGN007

Generating employee code like EMP007 in SQLSERVER2008

 Code Snippet posted by: CGN007 | Posted on: 6/7/2012 | Category: SQL Server Codes | Views: 679 | Status: [Member] | Points: 40 | Alert Moderator   


Generating employee code like EMP007 in SQLSERVER2008

DECLARE @employeecount INT

DECLARE @empid VARCHAR(10)
DECLARE @tempid INT
SELECT @employeecount= COUNT(*) FROM emp WHERE emp_id LIKE'EMP%'
IF @employeecount=0
SELECT @empid='EMP001';
ELSE
BEGIN
SELECT @tempid =MAX(CAST(REPLACE(emp_id,'EMP','')AS INT )+1 ) FROM emp;
IF LEN(@tempid)=1
SELECT @empid='EMP00'+CAST(@tempid AS VARCHAR(10));
IF LEN(@tempid)=2
SELECT @empid='EMP0'+CAST(@tempid AS VARCHAR(10));
IF LEN(@tempid)>2
SELECT @empid='EMP'+CAST(@tempid AS VARCHAR(10));
END
PRINT @empid;


Table Schema
CREATE TABLE [dbo].[emp](

[emp_id] [varchar](50) NULL,
[emp_name] [varchar](50) NULL
)

Found interesting? Add this to:


>> Write Response - Respond to this post and get points

More codes snippets

About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you find plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/21/2013 4:03:04 PM