Additional SQL Server features and topics not covered by specific categories
Try something like this in SQL:
declare @OurCode varchar(max) = 'ABC'
declare @max_code int = coalesce((select max(substring(left(RefNo, len(@OurCode)+1+3), len(@OurCode)+2, 1000)) from OurTable), 0)
declare @year int = year(getdate()) % 100
declare @fin_year varchar(max)
if month(getdate()) >= 4
set @fin_year = format(@year, '00') + '/' + format(@year + 1, '00')
else
set @fin_year = format(@year - 1, '00') + '/' + format(@year, '00')
declare @next_RefNo varchar(max) = @OurCode + '-' + format(@max_code + 1, '000') + '-' + @fin_year
Use the @next_RefNo to insert the new record. You can make some stored procedures. However, make sure that this code is not used incorrectly by multiple tasks in parallel. Otherwise, find other approaches.