Begin Transaction Declare @OOT_Prefix varchar(10); Declare @OOT_Counter integer; Declare @Current_MAX_OOT varchar(16); Declare @NEW_OOT varchar(16); -- Set the OOT number to start from SET @OOT_Counter = '1000'; --Build a new OOT Prefix if there is no OOT to start with; --We need an IF statement and exit process here. set @OOT_Prefix = 'OOT'+format(getdate(), 'yyyyMM')+'_'; --Now we get the current latest OOT Number Complete set @Current_MAX_OOT = (select max(C2314) from mt.calibration); --Now the fun begins, we have to strip out the integer value at the end -- EXanmple- set temp_number = (select substring(temp_asset, prefix_len+1, 4)+1); set @OOT_Counter = (select substring(@Current_MAX_OOT, 11, 4)); --Now we have the number and we add 1 Set @OOT_Counter = @OOT_Counter + 1; --Now we build the new OOT Number and Cast the number to a string set @NEW_OOT = 'OOT'+format(getdate(), 'yyyyMM')+'_' + Cast(@OOT_Counter as varchar(4)); select @NEW_OOT; --Amazing update mt.calibration set C2314 = @NEW_OOT where CTAG = '$asset_ctag$'; Commit transaction;