Click here to Skip to main content
15,903,856 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How we can generate a alpha-numeric CustomerID in sql server. For example, SC-12-10-26-0001, here SC is fix for every customer, 12 is showinng current year,10 is showing current month, 26 is showing current date and 0001 is showing first cutomer of the current date. I want to generate ID according to this. If new year, month or date is coming then it's change accroding to this. Pl'z guide me how we can solve this.......
Posted
Updated 18-Apr-17 19:38pm

something along these lines. here is a link aswell
SQL
CREATE
 TABLE
 YourTable (
  ID    AS  'SC'
 + convert(varchar, getdate(), 101)
 + RIGHT('0000' + convert(varchar,ID),6))


here is a link aswell

http://social.msdn.microsoft.com/Forums/en/transactsql/thread/1e3ada09-492b-46a2-9f65-c0f3990e2343[^]
 
Share this answer
 
v2
Comments
Sumit_Kumar_Sinha 26-Oct-12 2:54am    
my requirement is something different.....second last in the id is showing the current date and last one is customer id of that date....for next date second last date is change and last one again start from 0001
Hi,

See the below query it might be help you.

SQL
Declare @LastDigit NVARCHAR(5)
Select @LastDigit = ISNULL(Substring(CustomerID,13,len(CustomerID)),0) from dbo.Table_9
Set @LastDigit = MAX(Convert(Bigint,ISNULL(@LastDigit,0))) + 1
Set @LastDigit = RIGHT('000' + convert(varchar,@LastDigit),4)
Declare @CustomerID Nvarchar(50)
Set @CustomerID = 'SC-' +  substring(convert(varchar,Datepart(yy,GETDATE())),3,4)
+ '-' +  convert(varchar,Datepart(mm,GETDATE())) + '-' +
convert(varchar,Datepart(dd,GETDATE()))
+ '-' + @LastDigit
print(@CustomerID)


Thanks,
Viprat
 
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