Here is my code
Create function [dbo].[CASNo]()
returns varchar(20)
as
begin
DECLARE @lastval INT
SET @lastval = (select MAX(Cast(right(CAS_No,4) as varchar(10))) from tblCASdetails)
if @lastval is null set @lastval = 0
Return 'CAS-' + right('000' + convert(varchar(50),(@lastval + 1)),10)
end