sql union

I have below sql. and if I take out UNION. All works fine.
If I only two 2nd and 3 rd (combined together). It works fine as well.

But when I combined all three. they are not working as shown error like (Error Converting Data Type varchar to numeric
why? and how to fix it?


SELECT '0' AS LEV, C1105 AS DealerName, C1100 AS CustomerName, C1025 AS Probability, SUBSTRING(MJ007,1,30) AS StatusName, C1004 AS QuoteNo,C1005 AS Seq, CONVERT(char,C1022) AS ExpClose, C2007 AS ModelNo, C2010 AS Qty, C2014 AS UnitPrice, C2015 AS ExtnPrice, C1031 AS SubTotal FROM JC1 INNER JOIN JMJ ON C1013 = MJ006 AND MJ004 = 'JS' AND MJ005 = 210 LEFT OUTER JOIN JN6 ON C1101 = N6004 LEFT OUTER JOIN JN8 ON C1018 = N8004 INNER JOIN JC2 ON C1004 = C2004 AND C1005 = C2005 WHERE C1055 = '31' AND C1024 >= 20150901 AND C1024 <= 20150930 and C1013 = '02'
 
UNION
SELECT '1' AS LEV, C1105 AS DealerName, '99&' AS CustomerName, '99&' AS Probability, '99&' AS StatusName, '99&' AS QuoteNo,'99&' AS Seq, '99&' AS ExpClose, '99&' AS ModelNo, Sum(C2010) AS Qty, Sum(C2014) AS UnitPrice, Sum(C2015) AS ExtnPrice, Sum(C1031) AS SubTotal FROM JC1 INNER JOIN JMJ ON C1013 = MJ006 AND MJ004 = 'JS' AND MJ005 = 210 LEFT OUTER JOIN JN6 ON C1101 = N6004 LEFT OUTER JOIN JN8 ON C1018 = N8004 INNER JOIN JC2 ON C1004 = C2004 AND C1005 = C2005 WHERE C1055 = '31' AND C1024 >= 20150901 AND C1024 <= 20150930 and C1013 = '02' GROUP BY C1105

UNION
SELECT '2' AS LEV, '99&' AS DealerName, '99&' AS CustomerName, '99&' AS Probability, '99&' AS StatusName, '99&' AS QuoteNo,'99&' AS Seq, '99&' AS ExpClose, '99&' AS ModelNo, Sum(C2010) AS Qty, Sum(C2014) AS UnitPrice, Sum(C2015) AS ExtnPrice, Sum(C1031) AS SubTotal FROM JC1 INNER JOIN JMJ ON C1013 = MJ006 AND MJ004 = 'JS' AND MJ005 = 210 LEFT OUTER JOIN JN6 ON C1101 = N6004 LEFT OUTER JOIN JN8 ON C1018 = N8004 INNER JOIN JC2 ON C1004 = C2004 AND C1005 = C2005 WHERE C1055 = '31' AND C1024 >= 20150901 AND C1024 <= 20150930 and C1013 = '02' ORDER BY DealerName,LEV
LVL 1
ITsolutionWizardAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Éric MoreauSenior .Net ConsultantCommented:
that means that the datatype of (at least) one column is not the same between the first query and the other 2.

try commenting out columns until you find the one that causes the issue
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ITsolutionWizardAuthor Commented:
I've requested that this question be deleted for the following reason:

no one answer
0
Vitor MontalvãoMSSQL Senior EngineerCommented:
How do you mean no one answered?
I can see Éric's comment.
0
Vikas GargBusiness Intelligence DeveloperCommented:
Hello,

You can try this

SELECT '0' AS LEV, CONVERT(VARCHAR(50),C1105) AS DealerName, CONVERT(VARCHAR(50),C1100) AS CustomerName, CONVERT(VARCHAR(50),C1025) AS Probability, SUBSTRING(CONVERT(VARCHAR(50),MJ007),1,30) AS StatusName, 
CONVERT(VARCHAR(50),C1004) AS QuoteNo,CONVERT(VARCHAR(50),C1005) AS Seq, CONVERT(char,C1022) AS ExpClose, CONVERT(VARCHAR(50),C2007) AS ModelNo, C2010 AS Qty, C2014 AS UnitPrice, C2015 AS ExtnPrice, C1031 AS SubTotal 
FROM JC1 
INNER JOIN JMJ ON C1013 = MJ006 AND MJ004 = 'JS' AND MJ005 = 210 
LEFT OUTER JOIN JN6 ON C1101 = N6004 
LEFT OUTER JOIN JN8 ON C1018 = N8004 
INNER JOIN JC2 ON C1004 = C2004 AND C1005 = C2005 
WHERE C1055 = '31' AND C1024 >= 20150901 AND C1024 <= 20150930 and C1013 = '02'
 
UNION
SELECT '1' AS LEV, C1105 AS DealerName, '99&' AS CustomerName, '99&' AS Probability, '99&' AS StatusName, 
'99&' AS QuoteNo,'99&' AS Seq, '99&' AS ExpClose, '99&' AS ModelNo, Sum(C2010) AS Qty, Sum(C2014) AS UnitPrice, Sum(C2015) AS ExtnPrice, Sum(C1031) AS SubTotal 
FROM JC1 
INNER JOIN JMJ ON C1013 = MJ006 AND MJ004 = 'JS' AND MJ005 = 210 
LEFT OUTER JOIN JN6 ON C1101 = N6004 
LEFT OUTER JOIN JN8 ON C1018 = N8004 
INNER JOIN JC2 ON C1004 = C2004 AND C1005 = C2005 
WHERE C1055 = '31' AND C1024 >= 20150901 AND C1024 <= 20150930 and C1013 = '02' GROUP BY C1105

UNION
SELECT '2' AS LEV, '99&' AS DealerName, '99&' AS CustomerName, '99&' AS Probability, '99&' AS StatusName, 
'99&' AS QuoteNo,'99&' AS Seq, '99&' AS ExpClose, '99&' AS ModelNo, Sum(C2010) AS Qty, Sum(C2014) AS UnitPrice, Sum(C2015) AS ExtnPrice, Sum(C1031) AS SubTotal 
FROM JC1 
INNER JOIN JMJ ON C1013 = MJ006 AND MJ004 = 'JS' AND MJ005 = 210 
LEFT OUTER JOIN JN6 ON C1101 = N6004 
LEFT OUTER JOIN JN8 ON C1018 = N8004 
INNER JOIN JC2 ON C1004 = C2004 AND C1005 = C2005 
WHERE C1055 = '31' AND C1024 >= 20150901 AND C1024 <= 20150930 and C1013 = '02' ORDER BY DealerName,LEV 

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
SSRS

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.