title: Filtering stored procedure records by nested select case statement
body: <p>I need to further refine my stored proc resultset from <a href="https://stackoverflow.com/q/25767993/3929962">this post</a>, I need to filter my resultset to display only records where emailaddr is NULL (meaning display only records that have <code>Invoice_DeliveryType</code> value of 'N' ).  </p>
  
  <p>Among numerous queries, I have tried:  </p>
  
  <pre><code>select 
      Invoice_ID, 'Unknown' as Invoice_Status, 
      case when Invoice_Printed is null then '' else 'Y' end as Invoice_Printed, 
      case when Invoice_DeliveryDate is null then '' else 'Y' end as Invoice_Delivered, 
      (case when Invoice_DeliveryType &lt;&gt; 'USPS' then ''
            when exists (Select 1
                     from dbo.Client c
                     Where c.Client_ID = SUBSTRING(i.Invoice_ID, 1, 6) and
                           c.emailaddr is not null
                    )
            then 'Y'
            else 'N'
       end)
      Invoice_ContactLName + ', ' + Invoice_ContactFName as ContactName, 
  from 
      dbo.Invoice
  left outer join 
      dbo.fnInvoiceCurrentStatus() on Invoice_ID = CUST_InvoiceID 
  where 
      CUST_StatusID = 7 
      AND Invoice_ID = dbo.Client.Client_ID
      AND dbo.client.emailaddr is NULL
  order by 
      Inv_Created  
  </code></pre>
  
  <p>but I get an error </p>
  
  <blockquote>
    <p>The conversion of the nvarchar value '20111028995999' overflowed an int column</p>
  </blockquote>
  
  <p>How can I get the stored procedure to only return records with <code>DeliveryType = 'N'</code> ?</p>