Solved

SQL Union Error

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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
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 setup several different housekeeping processes for a SQL Server.

932 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

10 Experts available now in Live!

Get 1:1 Help Now