Solved

SQL Union Error

Posted on 2013-10-29
5
680 Views
Last Modified: 2013-10-29
I'm trying to create a report that shows both the actual budget numbers for each month.
Since I can't link them directly, i'm trying to use a union. Problem is that I'm getting the following error: ERROR converting data type varchar to numberic. Suggestions on how to get the following code to work?

select gla.COMPANY,
       gla.FISCAL_YEAR,
     --  '' AS BUDFY,
       gla.ACCT_UNIT,
       gla.ACCOUNT,
       gla.SUB_ACCOUNT,
       gla.DB_AMOUNT_01 + gla.CR_AMOUNT_01 GLAMONTH1, 
       gla.DB_AMOUNT_02 + gla.CR_AMOUNT_02 GLAMOUNT2,
       gla.DB_AMOUNT_03 + gla.CR_AMOUNT_03 GLAMONTH3,
       gla.DB_AMOUNT_04 + gla.CR_AMOUNT_04 GLAMONTH4,
       gla.DB_AMOUNT_05 + gla.CR_AMOUNT_05 GLAMONTH5,
       gla.DB_AMOUNT_06 + gla.CR_AMOUNT_06 GLAMONTH6,
       gla.DB_AMOUNT_07 + gla.CR_AMOUNT_07 GLAMONTH7,
       gla.DB_AMOUNT_08 + gla.CR_AMOUNT_08 GLAMONTH8, 
       gla.DB_AMOUNT_09 + gla.CR_AMOUNT_09 GLAMONTH9,
       gla.DB_AMOUNT_10 + gla.CR_AMOUNT_10 GLAMONTH10, 
       gla.DB_AMOUNT_11 + gla.CR_AMOUNT_11 GLAMONTH11,
       gla.DB_AMOUNT_12 + gla.CR_AMOUNT_12 GLAMONTH12,
       '' BUDMONTH1,
       '' BUDMONTH2,
       '' BUDMONTH3,
       '' BUDMONTH4,
       '' BUDMONTH5,
       '' BUDMONTH6,
       '' BUDMONTH7,
       '' BUDMONTH8,
       '' BUDMONTH9,
       '' BUDMONTH10,
       '' BUDMONTH11,
       '' BUDMONTH12
from GLAMOUNTS gla
where gla.ACCT_UNIT = 01000
and gla.ACCOUNT in (6050,6060)

union 

select fbd.COMPANY,
      -- '' AS GLFY,
       fbd.FISCAL_YEAR,
       fbd.ACCT_UNIT,
       fbd.ACCOUNT,
       fbd.SUB_ACCOUNT,
       '' GLAMONTH1,
       '' GLAMONTH2,
       '' GLAMONTH3,
       '' GLAMONTH4,
       '' GLAMONTH5,
       '' GLAMONTH6,
       '' GLAMONTH7,
       '' GLAMONTH8,
       '' GLAMONTH9,
       '' GLAMONTH10,
       '' GLAMONTH11,
       '' GLAMONTH12,
       fbd.DB_AMOUNT_01 + fbd.CR_AMOUNT_01 BUDMONTH1, 
       fbd.DB_AMOUNT_02 + fbd.CR_AMOUNT_02 BUDMOUNT2,
       fbd.DB_AMOUNT_03 + fbd.CR_AMOUNT_03 BUDMONTH3,
       fbd.DB_AMOUNT_04 + fbd.CR_AMOUNT_04 BUDMONTH4,
       fbd.DB_AMOUNT_05 + fbd.CR_AMOUNT_05 BUDMONTH5,
       fbd.DB_AMOUNT_06 + fbd.CR_AMOUNT_06 BUDMONTH6,
       fbd.DB_AMOUNT_07 + fbd.CR_AMOUNT_07 BUDMONTH7,
       fbd.DB_AMOUNT_08 + fbd.CR_AMOUNT_08 BUDMONTH8, 
       fbd.DB_AMOUNT_09 + fbd.CR_AMOUNT_09 BUDMONTH9,
       fbd.DB_AMOUNT_10 + fbd.CR_AMOUNT_10 BUDMONTH10, 
       fbd.DB_AMOUNT_11 + fbd.CR_AMOUNT_11 BUDMONTH11,
       fbd.DB_AMOUNT_12 + fbd.CR_AMOUNT_12 BUDMONTH12
from FBDETAIL fbd
where fbd.ACCT_UNIT = 01000
and fbd.ACCOUNT in (6050,6060)

Open in new window

0
Comment
Question by:metalteck
5 Comments
 
LVL 40

Accepted Solution

by:
Kyle Abrahams earned 400 total points
ID: 39609914
'' GLAMONTH1,
       '' GLAMONTH2,
       '' GLAMONTH3,
       '' GLAMONTH4,
       '' GLAMONTH5,
       '' GLAMONTH6,
       '' GLAMONTH7,
       '' GLAMONTH8,
       '' GLAMONTH9,
       '' GLAMONTH10,
       '' GLAMONTH11,
       '' GLAMONTH12,

should be 0 for each in the second part.
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 39609916
>ERROR converting data type varchar to numberic.

SELECT 'banana'
UNION
SELECT 42

will result in the same kind of error.  
You'll have to go through both sides of the UNION and make sure each column both has the same data type:  number above the UNION matches number below the union, dates to dates, bits to bits, etc.
0
 
LVL 15

Expert Comment

by:pateljitu
ID: 39609920
Try this query, changed ordering of second SQL query from

fbd.COMPANY,
-- '' AS GLFY,  
       fbd.FISCAL_YEAR,

to

fbd.COMPANY,
       fbd.FISCAL_YEAR,
-- '' AS GLFY,  

hence the UNION fails.

select gla.COMPANY,
       gla.FISCAL_YEAR,
     --  '' AS BUDFY,
       gla.ACCT_UNIT,
       gla.ACCOUNT,
       gla.SUB_ACCOUNT,
       gla.DB_AMOUNT_01 + gla.CR_AMOUNT_01 GLAMONTH1,
       gla.DB_AMOUNT_02 + gla.CR_AMOUNT_02 GLAMOUNT2,
       gla.DB_AMOUNT_03 + gla.CR_AMOUNT_03 GLAMONTH3,
       gla.DB_AMOUNT_04 + gla.CR_AMOUNT_04 GLAMONTH4,
       gla.DB_AMOUNT_05 + gla.CR_AMOUNT_05 GLAMONTH5,
       gla.DB_AMOUNT_06 + gla.CR_AMOUNT_06 GLAMONTH6,
       gla.DB_AMOUNT_07 + gla.CR_AMOUNT_07 GLAMONTH7,
       gla.DB_AMOUNT_08 + gla.CR_AMOUNT_08 GLAMONTH8,
       gla.DB_AMOUNT_09 + gla.CR_AMOUNT_09 GLAMONTH9,
       gla.DB_AMOUNT_10 + gla.CR_AMOUNT_10 GLAMONTH10,
       gla.DB_AMOUNT_11 + gla.CR_AMOUNT_11 GLAMONTH11,
       gla.DB_AMOUNT_12 + gla.CR_AMOUNT_12 GLAMONTH12,
       '' BUDMONTH1,
       '' BUDMONTH2,
       '' BUDMONTH3,
       '' BUDMONTH4,
       '' BUDMONTH5,
       '' BUDMONTH6,
       '' BUDMONTH7,
       '' BUDMONTH8,
       '' BUDMONTH9,
       '' BUDMONTH10,
       '' BUDMONTH11,
       '' BUDMONTH12
from GLAMOUNTS gla
where gla.ACCT_UNIT = 01000
and gla.ACCOUNT in (6050,6060)

union

select fbd.COMPANY,
       fbd.FISCAL_YEAR,
-- '' AS GLFY,      
 fbd.ACCT_UNIT,
       fbd.ACCOUNT,
       fbd.SUB_ACCOUNT,
       '' GLAMONTH1,
       '' GLAMONTH2,
       '' GLAMONTH3,
       '' GLAMONTH4,
       '' GLAMONTH5,
       '' GLAMONTH6,
       '' GLAMONTH7,
       '' GLAMONTH8,
       '' GLAMONTH9,
       '' GLAMONTH10,
       '' GLAMONTH11,
       '' GLAMONTH12,
       fbd.DB_AMOUNT_01 + fbd.CR_AMOUNT_01 BUDMONTH1,
       fbd.DB_AMOUNT_02 + fbd.CR_AMOUNT_02 BUDMOUNT2,
       fbd.DB_AMOUNT_03 + fbd.CR_AMOUNT_03 BUDMONTH3,
       fbd.DB_AMOUNT_04 + fbd.CR_AMOUNT_04 BUDMONTH4,
       fbd.DB_AMOUNT_05 + fbd.CR_AMOUNT_05 BUDMONTH5,
       fbd.DB_AMOUNT_06 + fbd.CR_AMOUNT_06 BUDMONTH6,
       fbd.DB_AMOUNT_07 + fbd.CR_AMOUNT_07 BUDMONTH7,
       fbd.DB_AMOUNT_08 + fbd.CR_AMOUNT_08 BUDMONTH8,
       fbd.DB_AMOUNT_09 + fbd.CR_AMOUNT_09 BUDMONTH9,
       fbd.DB_AMOUNT_10 + fbd.CR_AMOUNT_10 BUDMONTH10,
       fbd.DB_AMOUNT_11 + fbd.CR_AMOUNT_11 BUDMONTH11,
       fbd.DB_AMOUNT_12 + fbd.CR_AMOUNT_12 BUDMONTH12
from FBDETAIL fbd
where fbd.ACCT_UNIT = 01000
and fbd.ACCOUNT in (6050,6060) 

Open in new window

0
 
LVL 65

Assisted Solution

by:Jim Horn
Jim Horn earned 100 total points
ID: 39609936
I believe ged325 caught it.  

SQL is going to interpret '' as a char of some sort, so that if you then UNION it to a number it throws the error.  The only way around this is to change '' to a number, say 0, or CAST(the DB_AMOUNT expression) as a char.
0
 
LVL 26

Expert Comment

by:Shaun Kline
ID: 39609941
One other item...
In your WHERE clause, you have "ACCT_UNIT = 01000". As most numbers do not start with a zero, should the value be enclosed in apostrophes, like this:

ACCT_UNIT = '01000'
0

Featured Post

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how the fundamental information of how to create a table.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question