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 7571053
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T15:34:36+00:00 2026-05-30T15:34:36+00:00

I want to generate a unique id for a student based on the department

  • 0

I want to generate a unique id for a student based on the department of the student.
I want student Id to be CS0448 when department id is CS,and IT0448 for department id IT and so on.And these IDs must increment by 1 .

I thought that I will have another identity and autoincrement it by 1 for each student added and concatenate it with department Id,but that wont help me.It will cause IT student added next to CS0448 to be IT0449. Please help me with this. see this…it must be something like this..

...
cs0439
it0441
cs0440
it0442
cs0441
cs0442

Please let me know the better options for a alphanumeric identity

  • 1 1 Answer
  • 0 Views
  • 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-30T15:34:37+00:00Added an answer on May 30, 2026 at 3:34 pm

    You could try an INSTEAD OF INSERT trigger, like this:

    create table students(id varchar(10) primary key not null, Department varchar(2), Name varchar(80))
    go
    
    create trigger students_insert_PK
        on students
        INSTEAD OF INSERT
    as
        declare @id int;
        declare @dept varchar(2); select @dept=Department from INSERTED;
        select @id=cast(max(right(id,4)) as int) from students where Department=@dept;
        set @id=isnull(@id,0)+1;
    
        insert into students
        select Department+right('0000'+cast(@id as varchar(4)),4)
        , Department
        , name
        from INSERTED;
    go
    
    insert into students (Department,Name) values ('CS','John');
    insert into students (Department,Name) values ('CS','Pat');
    insert into students (Department,Name) values ('CS','Sheryl');
    insert into students (Department,Name) values ('IT','Phil');
    insert into students (Department,Name) values ('EE','Frank');
    insert into students (Department,Name) values ('EE','Amy');
    insert into students (Department,Name) values ('EE','Stu');
    go
    
    select * from students;
    go
    

    Results:

    enter image description here

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

Sidebar

Related Questions

I have an XML document that I want to generate unique IDs for. Some
In my .bat file I want to generate a unique name for files/directories based
I want to generate a list of unique IDs. Because some of the IDs
I want to generate unique random numbers and add item in function of these
Is it possible to generate a unique ID based on server hardware/OS? I want
I want to generate unique and random numbers or IDs which I can use
I was working on a servlet that will generate a unique code and update
I want to generate a unique id which will be used in URLs to
I want to auto-generate a unique 8-10 character ID string that includes a checksum
I want to generate unique random, N-valued key. This key can contain numbers and

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.