Reference:
SELECT LEN((REPLACE(REPLACE(REPLACE(FORMAT(SYSDATETIME(), 'yyyyMMddHHmmss.fff'), '-', ''), ':', ''), '.', ''))) AS [Length], REPLACE(REPLACE(REPLACE(FORMAT(SYSDATETIME(), 'yyyyMMddHHmmss.fff'), '-', ''), ':', ''), '.', '') + LEFT(CAST(NEWID() AS VARCHAR(36)), 3) AS First20Characters;
**
Requirements:**
-
Length should be 20;
-
should contain datetime stamp;
-
should not create duplicates, as bulk data would be inserted here. So there could be a situation data can be inserted in same interval of time: miliseconds or in microseconds.
-
(FORMAT(SYSDATETIME(), 'yyyyMMddHHmmss.fff')
this is creating 17 digits need to have more 3 digits.
give me some ideas to fulfill the requirements/ I need 20 digits unique for 10000 data getting input at once.
-
Should be Numeric not Alphanumeric .
-
do not use GuidID(), Rand() or newID type function. as datetime stamp is being used to make it unique.
It will take time to replace and remove char as seen in Reference. -
SELECT LEN((REPLACE(REPLACE(REPLACE(FORMAT(SYSDATETIME(), 'yyyyMMddHHmmss.fff'), '-', ''), ':', ''), '.', ''))) AS [Length], REPLACE(REPLACE(REPLACE(FORMAT(SYSDATETIME(), 'yyyyMMddHHmmss.fff'), '-', ''), ':', ''), '.', '') + LEFT(CAST(NEWID() AS VARCHAR(36)), 3) AS First20Characters;
no use here i have used NEWID() to create.
-
@newID = REPLACE(REPLACE(REPLACE(FORMAT(SYSDATETIME(), 'yyyyMMddHHmmss'), '-', ''), ':', ''), '.', '') + RIGHT('000000' + CAST(@identity AS VARCHAR(6)), 6);
Identity is not working: because here 14 digits is being comsumed by sysdatetime andlater 7 digits will be not sufficient if daily bulk data is being inserted. NOT A GOOD IDEA.
my activites is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.