Jes Sie

Jes Sie

  • 708
  • 1.2k
  • 265.3k

Concatenating sql table rows and converting a data type

Mar 26 2017 10:38 AM
I'm concatenating multiple table rows into one row. Below code is the working only with nvarchar data type but not with int.
  1. ALTER PROCEDURE [dbo].[spViewConcatenatedPolicyCInoForTaxInvoice_motorOnly]  
  2.     -- Add the parameters for the stored procedure here  
  3.     @TaxInvoiceNumber nvarchar(50)  
  4. AS  
  5. BEGIN  
  6.       
  7.         DECLARE @MotorCINo int  
  8.         SET @MotorCINo=''  
  9.    
  10.         SELECT @MotorCINo = @MotorCINo + MotorCINo +', '  
  11.         FROM    CashierSalesDetails  
  12.         WHERE TaxInvoiceNumber = @TaxInvoiceNumber  
  13.         SELECT  LEFT(@MotorCINo,LEN(@MotorCINo)-1) AS PolicyCINo  
  14.   
  15. END  
 below is the error:
  1. Msg 245, Level 16, State 1, Procedure spViewConcatenatedPolicyCInoForTaxInvoice_motorOnly, Line 17  
  2. Conversion failed when converting the varchar value ', ' to data type int.  
 Any is greatly appreciated.

Answers (4)