Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1078985
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T21:48:13+00:00 2026-05-16T21:48:13+00:00

I would like suggestions how to solve the following demand. I have a table

  • 0

I would like suggestions how to solve the following demand.

I have a table with three fields: EmployeeId, HireDate and DepartmentId.

The EmployeeId needs to be in the following format: yyyyddddxxxx
where:

  yyyy - is the year the employee was hired.
  dddd - DepartmentId.
  xxxx - running number for each department on each year.

Would this be best computed using sql or C#?
How do I calculate the ‘xxxx’ part?
I can add more fields to the table if needed.

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T21:48:14+00:00Added an answer on May 16, 2026 at 9:48 pm

    Magic!

    ;with Employees (EmployeeId, HireDate, DepartmentId) as
    (
     select 1, getdate()-10, 1 union 
     select 2, getdate()-10, 1 union 
     select 3, getdate()-8, 2 union 
     select 4, getdate()-7, 3 union 
     select 5, getdate()-6, 1  
    )
    select cast(datepart(year, HireDate) as varchar(4)) + 
     right(replicate('0' ,4)+cast(DepartmentId as varchar(4)), 4) +
     right(replicate('0' ,4)+cast(row_number() over (partition by DepartmentId,datepart(year, HireDate) order by HireDate asc) as varchar(4)), 4) EmployeeCode
     ,DepartmentId
     ,EmployeeId 
     ,convert(varchar(10), HireDate, 120) HireDate
    from Employees
    

    Will give us the following:

    EmployeeCode DepartmentId EmployeeId  HireDate
    ------------ ------------ ----------- ----------
    201000010001 1            1           2010-09-05
    201000010002 1            2           2010-09-05
    201000010003 1            5           2010-09-09
    201000020001 2            3           2010-09-07
    201000030001 3            4           2010-09-08
    

    UPDATE:

    Now supposing you want to add a new employee to the Department #2 today. Here’s how I’d calculate a new EmployeeCode for this employee:

    declare @DepartmentId int
    set @DepartmentId = 2
    select 
     cast(datepart(year, getdate()) as varchar(4)) + 
     right(replicate('0' ,4)+cast(@DepartmentId as varchar(4)), 4) +
     right(replicate('0' ,4)+cast(isnull(max(cast(right(EmployeeCode,4) as smallint)),0) + 1 as varchar(4)), 4) EmployeeCode
    from dbo.Employees as e
    where DepartmentId = @DepartmentId 
    and datepart(year, hiredate) = datepart(year, getdate())
    

    UPDATE:

    As you can see, if you add an employee to a hitherto nonexistent department, say, #200, then the max clause would return null as there are no employees in this department and it’d get isnulled to 0 + 1, so you’d get a perfectly normal 201002000001 for that employee.

    Suppose a year passed and now it’s 2011 and this last filtering clause again would null the max clause and the procedure would repeat and we’d get 201102000001 for a new employee in that new department this next-year.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some more questions regarding powerpoint masters, preferably I would like to solve
I need some asp.net pagination code samples. I would like suggestions on open source
I would like your suggestions on how to proceed in making a control panel
I have a little experience with WCF and would like to get your opinion/suggestion
I would like some sugestion on how to design a table that gets like
Would like to parse IPv4 address from exit-addresses . Format of the file: ExitNode
I have a UIImage and I would like to put its data in a
I have a rails site and I would like to render the live site
I would like to have a generic Interpolator class which can interpolate between instances
I have a Window where I have put a Frame. I would like to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.