title: Error converting data type varchar to bigint in stored procedure
body: <p>I'm trying to call this procedure with the <code>usp_TimesheetsAuditsLoadAllbyId 42747, NULL</code> command.</p>
  
  <p>But I always get an error</p>
  
  <blockquote>
    <p>Msg 8114, Level 16, State 5, Procedure usp_TimesheetsAuditsLoadAllById, Line 9<br>
    Error converting data type varchar to bigint.</p>
  </blockquote>
  
  <p>The <code>ID</code> of <code>TimesheetsAudits</code> table is a <code>bigint</code> type. I tried several types of conversions and casts, but I'm really stuck right now.  </p>
  
  <p>Hope somebody can help. Thanks  </p>
  
  <pre><code>ALTER PROCEDURE [dbo].[usp_TimesheetsAuditsLoadAllById]
  (
      @Id INT,
      @StartDate DATETIME
  )
  AS
  BEGIN
     SET NOCOUNT ON
  
     SELECT TOP 51 * 
     FROM 
        (SELECT TOP 51 
            ID,
            Type, 
            ReferrerId,
            CAST(Description AS VARCHAR(MAX)) AS Description,
            OnBehalfOf,
            Creator,
            DateCreated 
         FROM 
            TimesheetsAudits 
         WHERE 
            (ReferrerID = @Id) AND
            (@StartDate IS NULL OR DateCreated &lt; @StartDate)
         ORDER BY
            DateCreated DESC
  
         UNION
  
         SELECT TOP 51 
            tia.ID,
            tia.Type, 
            tia.ReferrerId,
            '[Day: ' + CAST(DayNr AS VARCHAR(5)) + '] ' + CAST(tia.Description AS VARCHAR(MAX)) AS Description,
            tia.OnBehalfOf,
            tia.Creator,
            tia.DateCreated 
         FROM 
            TimesheetItemsAudits tia
         INNER JOIN 
            TimesheetItems ti ON tia.ReferrerId = ti.ID
         WHERE 
            (ti.TimesheetID = @Id) AND
            (@StartDate IS NULL OR tia.DateCreated &lt; @StartDate)
         ORDER BY 
            tia.DateCreated DESC) t
     ORDER BY 
         t.DateCreated DESC
  END
  </code></pre>
  
  <p>Table definition for tables from comments:</p>
  
  <pre><code>CREATE TABLE [dbo].[TimesheetsAudits]( 
    [ID] [bigint] IDENTITY(1,1) NOT NULL, 
    [Type] [tinyint] NOT NULL, 
    [ReferrerId] [varchar](15) NOT NULL, 
    [Description] [text] NULL, 
    [OnBehalfOf] [varchar](10) NULL, 
    [Creator] [varchar](10) NOT NULL, 
    [DateCreated] [datetime] NOT NULL
  )
  
  
  
  CREATE TABLE [dbo].[TimesheetItemsAudits]( 
    [ID] [bigint] IDENTITY(1,1) NOT NULL, 
    [Type] [tinyint] NOT NULL, 
    [ReferrerId] [varchar](15) NOT NULL, 
    [Description] [text] NULL, 
    [OnBehalfOf] [varchar](10) NULL, 
    [Creator] [varchar](10) NOT NULL, 
    [DateCreated] [datetime] NOT NULL
  )
  </code></pre>