Solved

SQL Union Error

Posted on 2013-10-29
5
657 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 39

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
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, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

707 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now