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

  • Home
  • SEARCH
  • 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 8747545
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:22:26+00:00 2026-06-13T12:22:26+00:00

I need to enter primary key in table as in below format YYYY/MMM/NNNNNN Where,

  • 0

I need to enter primary key in table as in below format

YYYY/MMM/NNNNNN

Where, YYYY is the current year, MMM is Month , and NNNNNN is a sequence no from 000001, 000002, 000003, .... 999999.

So my primary key will look like 2012/Oct/000001 or 2012/Oct/000010 ….

How can I generate this type of code..

I can get Year and Month from Getdate() function. But how can I manage sequence number on every insert. can you please give me logic for that?

  • 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-06-13T12:22:27+00:00Added an answer on June 13, 2026 at 12:22 pm

    By far the easiest way would be to let SQL Server handle the dishing out of consecutive numbers using an INT IDENTITY column – and then use a trigger to create that specific format that you need, in a separate column.

    So given this table:

    CREATE TABLE SampleTable 
                 (ID INT IDENTITY, SaleDate DATE, ComputedPK VARCHAR(25) )
    

    you could use a trigger like this to compute the ComputedPK from the ID (autonumber, handled by SQL Server) and the SaleDate as the date column:

    CREATE TRIGGER trgSampleTableInsert
    ON dbo.SampleTable FOR INSERT
    AS 
        UPDATE dbo.SampleTable
        SET ComputedPK = CAST(YEAR(i.SaleDate) AS CHAR(4)) + '/' + DATENAME(MONTH, i.SaleDate) + '/' + RIGHT('000000' + CAST(i.ID AS VARCHAR(6)), 6) 
        FROM dbo.SampleTable s
        INNER JOIN INSERTED i ON s.ID = i.ID
    

    This approach will not start at 1 for each month, however – but do you really need that? Isn’t a sequential number (even across months) good enough?

    Update: of course, if you’re using SQL Server 2012 (you didn’t specify which version of SQL Server you’re using…) – you could use a SEQUENCE object to handle the consecutive numbering – and you could even reset that sequence to 1 again every start of a month ….

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

Sidebar

Related Questions

I need to enter primary key values in MySql database something like the following.
Have a linking (or ref) table which has a dual primary key. Need to
I need to make the Enter key as Tab, changing the focus of the
I have a simple table with a primary key. Most of the read operations
I have an excel table - see attached screenshot. need enter an formula into
I have a requirement where i need to enter a text in the text
i have a text field in Contact screen and the user need to enter
My Unix password has timed out, and I need to enter a new one,
I need the user to enter a URL within my form. However, URL is
I need a form to enable user to enter event info in three fields

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.