title: SQL Server - The conversion of the varchar value overflowed an int column
body: <p>I need to generate string with random digits with specified length so I do:  </p>
  
  <pre><code>ALTER PROCEDURE [dbo].[Generate Account]
  AS
  BEGIN
      DECLARE @accountNumber VARCHAR(36)
      DECLARE @acc_1 BIGINT
      DECLARE @acc_2 BIGINT
      DECLARE @acc_3 BIGINT
      DECLARE @acc_4 BIGINT -- line 26
      DECLARE @acc_5 BIGINT
      DECLARE @acc_6 BIGINT
  
      SET @acc_1 = 1000000-ceiling(rand()*100000) + datepart(ms, getdate())
      SET @acc_2 = 1000000-ceiling(rand()*100000) + datepart(ms, getdate())
      SET @acc_3 = 1000000-ceiling(rand()*100000) + datepart(ms, getdate())
      SET @acc_4 = 1000000-ceiling(rand()*100000) + datepart(ms, getdate()) -- line 33
      SET @acc_5 = 1000000-ceiling(rand()*100000) + datepart(ms, getdate())
      SET @acc_6 = 1000000-ceiling(rand()*100000) + datepart(ms, getdate())
  
      SET @accountNumber = CONVERT(VARCHAR(6), @acc_1) + 
                              CONVERT(VARCHAR(6), @acc_2) + 
                              CONVERT(VARCHAR(6), @acc_3) + 
                              CONVERT(VARCHAR(6), @acc_4) + 
                              CONVERT(VARCHAR(6), @acc_5) + 
                              CONVERT(VARCHAR(6), @acc_6)
  
      INSERT INTO [dbo].[account] 
      (
          [identifier]
      ) VALUES (
          @accountNumber
      )
  
      RETURN @accountNumber
  END
  </code></pre>
  
  <p>But I'm getting error:  </p>
  
  <blockquote>
    <p>Msg 8152, Level 16, State 14, Procedure Generate Account, Line 26 [Batch Start   Line 2]<br>
    String or binary data would be truncated.<br>
    The statement has been terminated.  </p>
    
    <p>Msg 248, Level 16, State 1, Procedure Generate Account, Line 33 [Batch Start Line 2]<br>
    The conversion of the varchar value '976547932115913127987338933646998655' overflowed an int column.<br>
    The 'Generate Account' procedure attempted to return a status of NULL, which is not allowed. A status of 0 will be returned instead.</p>
  </blockquote>
  
  <p>Why? I have already changed <code>@acc_X</code> from INT to BIGINT but I still getting this error...</p>