The line
SELECT @Prefix + LEN(CAST(@maxCode AS VARCHAR(10))+1) + CAST(@maxCode AS VARCHAR(100))
is wrong.
@Prefix
is 'J'
and LEN(...anything...)
is an int
, hence the type mismatch.
It seems to me, you actually want to do,
SELECT
@maxCode = MAX(
CAST(SUBSTRING(
Voucher_No,
@startFrom + 1,
LEN(Voucher_No) - (@startFrom + 1)) AS INT)
FROM
dbo.Journal_Entry;
SELECT @Prefix + CAST(@maxCode AS VARCHAR(10));
but, I couldn't say. If you illustrated before and after data, it would help.