Click here to Skip to main content
15,915,062 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am a beginner to programming in vb.net and sql server. I want to save a column made up of a year plus a number with symbol "/" that is renewed every year like 19/0001, 19/0002, 20/0001, 20/0002 


What I have tried:

I am a beginner to programming in vb.net and sql server. I want to save a column made up of a year plus a number with symbol "/" that is renewed every year like 19/0001, 19/0002, 20/0001, 20/0002 
Posted
Updated 24-Jan-20 20:52pm

Quote:
How do I generate custom ID using year and integer in VB.NET

First of all, never generate an id on client side for an SQL server, because 1 day you will have 2 clients generating an id at same time and ending with 2 record with same id. Always have the id generated in server side.
The easiest id is a simple sequence generated by server:
SQL AUTO INCREMENT a Field[^]
SQL AUTO_INCREMENT - SQL[^]
MySQL :: MySQL 8.0 Reference Manual :: 3.6.9 Using AUTO_INCREMENT[^]
you can make it custom, but it get more complicated:
Custom Auto-Generated Sequences with SQL Server - SQLTeam.com[^]
may not fit exactly your needs but you get the idea.
 
Share this answer
 
In my opinion you have two, partially separated questions: How to identify a record and how to make it unique. Also you clearly want to use an identification which is meaningful to the user.

In order for your program to identify a record, the general suggestion is to use a Surrogate key - Wikipedia[^]. Since this kind of key is not derived from the data there is no risk that the value would need to be changed if the data or the requirements change.

To generate a surrogate key, you can use for example an IDENTITY[^] or a SEQUENCE[^]. If the system you're building is going to have multiple databases then an uniqueidentifier[^] may be a suitable choice. By defining this column as a Primary Key [^] you ensure that only one value can exist. in a table

What comes to the key visible to user, you can use Natural key - Wikipedia[^]. To ensure that there is only one record in the table you can define also this as unique using an Unique Constraint[^].

In your example you actually had two elements which together would be the natural key, the year and the running number. These should not be stored in a single column like you described. Instead you should use two columns, one for year and the other one for the ordinal. Making these to together unique guarantees you the correct key.

In your question you wanted to show these two items in a single column separated with a slash. Since the two items are stored in separate columns this should be done either in the SQL fetching the data or perhaps preferably on the clients side. To do this in SQL, consider the following example
SQL
select cast(a.year as varchar) + ' / ' + format( a.ordinal, '00000')
from ( select 2020 as year, 1 as ordinal union all
       select 2020 as year, 2 as ordinal union all
       select 2020 as year, 3 as ordinal ) a

This would give
2020 / 00001
2020 / 00002
2020 / 00003
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900