Conversion failed when converting the varchar value 'BB-10001' to data type int.

Analyst_SQL 3,576 Reputation points
2021-11-25T15:57:10.103+00:00

Below is data ,i am facing error

Conversion failed when converting the varchar value 'BB-10001' to data type int.

Create table #tbl_Bale_Prd (Bale_ID varchar(12),Item_Code int,Bale_Qty int ,Bale_Weight decimal(10,2),D_DID int)  
Create table #tbl_GRN_Detail (D_ID int,Item_Code int,Item_Qty int,Item_Weight decimal(10,2),D_DID int)  
  
  
Insert into #tbl_Bale_Prd values ('BB-10001',1,1,555,null)  
Insert into #tbl_Bale_Prd values ('BB-10002',2,1,410,null)  
Insert into #tbl_Bale_Prd values ('BB-10003',3,1,545,null)  
  
  
Insert into #tbl_GRN_Detail values (12001,1,1,555,null)  
Insert into #tbl_GRN_Detail values (12002,6,1,451,null)  
Insert into #tbl_GRN_Detail values (12003,8,1,980,null)  
  
  
  
select Bale_ID,item_code,Bale_Qty,Bale_Weight,D_DID From #tbl_Bale_Prd  where  Bale_ID = 'BB-10001' and (D_DID IS NULL or D_DID=1)  
  
union   
  
Select CONVERT(INT, CONVERT(VARCHAR(15), D_ID))  as Bale_ID,item_code,Item_Qty as Bale_Qty,Item_Weight as Bale_WEight,D_DID from #tbl_GRN_Detail    
where  D_ID = 'BB-10001' and (D_DID IS NULL or D_DID=1)  
  

Desire Output

152684-image.png

Developer technologies Transact-SQL
SQL Server Other
{count} votes

Accepted answer
  1. Olaf Helper 47,436 Reputation points
    2021-11-26T07:24:00.337+00:00
     select Bale_ID, ...      
     union       
     Select CONVERT(INT, CONVERT(VARCHAR(15), D_ID))  
    

    Bale_ID is varchar, in the secon SELECT you convert a varchar to integer and because of Data type precedence (Transact-SQL) SQL Server now converts BaleID also to integer, what of course fails.
    Remove the integer conversion.

    1 person found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. Yitzhak Khabinsky 26,586 Reputation points
    2021-11-25T18:11:42.987+00:00

    Hi @

    Please try the following solution.

    SQL

    -- DDL and sample data population, start
     DECLARE @tbl_Bale_Prd TABLE (
        Bale_ID varchar(12),
        Item_Code int,
        Bale_Qty int,
        Bale_Weight decimal(10,2),
        D_DID int);
    
     DECLARE @tbl_GRN_Detail TABLE (
        D_ID INT,
        Item_Code int,
        Item_Qty int,
        Item_Weight decimal(10,2),
        D_DID int);
    
    INSERT INTO @tbl_Bale_Prd VALUES 
    ('BB-10001',1,1,555,null),
    ('BB-10002',2,1,410,null),
    ('BB-10003',3,1,545,null);
    
    INSERT INTO @tbl_GRN_Detail VALUES 
    (12001,1,1,555,null),
    (12002,6,1,451,null),
    (12003,8,1,980,null);
    -- DDL and sample data population, end
    
    
    DECLARE @Bale_ID VARCHAR(12) = 'BB-10001';
    
     ;WITH rs AS
     (
        Select 'BB-' + TRY_CAST(D_ID AS VARCHAR(12))  as Bale_ID
            ,item_code,Item_Qty as Bale_Qty
            ,Item_Weight as Bale_WEight
            ,D_DID 
        FROM @tbl_GRN_Detail  
    )   
    SELECT Bale_ID
        , item_code
        , Bale_Qty,Bale_Weight
        , D_DID 
     FROM @tbl_Bale_Prd  
     WHERE  Bale_ID = @Bale_ID and (D_DID IS NULL or D_DID=1)
     UNION ALL
     SELECT Bale_ID
        , item_code
        , Bale_Qty
        , Bale_WEight
        , D_DID 
     FROM rs
     WHERE Bale_ID = @Bale_ID and (D_DID IS NULL or D_DID=1);
    

  2. EchoLiu-MSFT 14,621 Reputation points
    2021-11-26T06:08:14.543+00:00

    Please also check:

     ;with cte  
     as(select Bale_ID,item_code,Bale_Qty,Bale_Weight,D_DID   
     From #tbl_Bale_Prd    
     where  Bale_ID = 'BB-10001' and (D_DID IS NULL or D_DID=1)     
     union      
     Select CONVERT(VARCHAR(15), D_ID) as Bale_ID,  
     item_code,Item_Qty as Bale_Qty,Item_Weight as Bale_WEight,D_DID   
     from #tbl_GRN_Detail)  
       
     select * from cte  
     where  Bale_ID= 'BB-10001' and (D_DID IS NULL or D_DID=1)  
    

    Output:
    152795-image.png

    Regards,
    Echo


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.