To create a unique number in SQL Server, you can use various techniques depending on your requirements. Here are a few approaches you can consider.
- Identity Column
- UUID (Universally Unique Identifier)
- User Definer Unique Code as below
-
declare @vch_UniqueRefNo varchar(30)='',@slno int=0
select @slno=count(int_req_payment_id) from T_COP_REQUEST_PAYMENTS where convert(date,dt_PaymentDate)=convert(date,getdate());
set @slno = @slno + 1;
set @vch_UniqueRefNo= convert(varchar,year(GETDATE()))+format(month(GETDATE()),'00')+format(day(GETDATE()),'00')+format(@slno,'0000')
select @vch_UniqueRefNo
- Output