title: Conversion failed when converting the varchar value to data type int in sql
body: <p>I wrote the store procedure which should return the  values like-</p>
  
  <pre><code>J1
  J2
  J3
  </code></pre>
  
  <p>I have table named <code>Journal_Entry</code>. When the row count of the table is 0, it gives the result <code>J1</code> but as the row count increases it shows the error-</p>
  
  <pre><code>"Conversion failed when converting the varchar value 'J' to data type int."
  </code></pre>
  
  <p>#here the <code>Voucher_No</code> is the column for the result to be saved.  </p>
  
  <p>The code is like-</p>
  
  <pre><code>CREATE PROC [dbo].[getVoucherNo]
  AS
  BEGIN
      DECLARE @Prefix VARCHAR(10)='J'
      DECLARE @startFrom INT=1
      DECLARE @maxCode VARCHAR(100)
      DECLARE @sCode INT
      IF((SELECT COUNT(*) FROM dbo.Journal_Entry) &gt; 0)
      BEGIN
          SELECT @maxCode = CAST(MAX(CAST(SUBSTRING(Voucher_No,LEN(@startFrom)+1,LEN(Voucher_No)- LEN(@Prefix)) AS INT)) AS varchar(100)) FROM dbo.Journal_Entry;
          SET @sCode=CAST(@maxCode AS INT)
          SELECT  @Prefix + LEN(CAST(@maxCode AS VARCHAR(10))+1) + CAST(@maxCode AS VARCHAR(100))
      END
      ELSE
      BEGIN
          SELECT(@Prefix + CAST(@startFrom AS VARCHAR)) 
      END
  
  END
  </code></pre>