Click here to Skip to main content
15,891,828 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi
I have a user define function as below, to compute a custom Id using PK Id as guide
 CREATE FUNCTION [dbo].[CustomeId] (
 @Prefix NVARCHAR(10),
 @Id INT,
 @Length INT,
 @PaddingChar CHAR(1) = '0'
)
RETURNS NVARCHAR(MAX)
AS
BEGIN

RETURN (
 SELECT @Prefix + RIGHT(REPLICATE(@PaddingChar, @Length) + CAST(@Id as nvarchar(10)), @Length)
)

END



In my sql table's Computed Column specification I have:
([dbo].[CustomeZoneId]('GH-',CONVERT([nvarchar](10),[Id],(0)),(10),'0'))


and out put is GH-0000000001 , GH-999999999
but I want it to be able to grow to infinity along side the PK Id. something like GH-1 to infinity

What I have tried:

([dbo].[CustomeZoneId]('GH-',CONVERT([nvarchar](10),[Id],(0)),(10),'0'))
Posted
Updated 14-Feb-17 11:43am
Comments
njammy 13-Feb-17 11:12am    
Can you not use a MS SQL Sequence object?
Dave Kreskowiak 14-Feb-17 19:58pm    
You cannot use "infinity" as a requirement. Infinit ID's require infinite storage, which no machine on earth has.

This is a business rule you're talking about. So what does the business expect to happen when the ID numbers "rollover"? That's where your answer lies.

1 solution

Use a guid?
tp://stackoverflow.com/questions/1435908/c-sharp-guid-and-sql-uniqueidentifier
 
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