简体   繁体   中英

I have an issue trying to UNION All in SQL Server 2008

I am having to create a second header line and am using the first record of the Query to do this. I am using a UNION All to create this header record and the second part of the UNION to extract the Data required. I have one issue on one column.

,'Active Energy kWh'

UNION ALL

,SUM(cast(invc.UNITS as Decimal (15,0)))

Each side are 11 lines before and after the Union and I have tried all sorts of combinations but it always results in an error message.

The above gives me "Error converting data type varchar to numeric."

Any help would be much appreciated.

The error message indicates that one of your values in the INVC table UNITS column is non-numeric. I would hazard a guess that it's either a string (VARCHAR or similar) column or something else - and one of the values has ended up in a state where it cannot be parsed.

Unfortunately there is no way other than checking small ranges of the table to gradually locate the 'bad' row (ie Try running the query for a few million rows at a time, then reducing the number until you home in on the bad data). SQL 2014 if you can get a database restored to it has the TRY_CONVERT function which will permit conversions to fail, enabling a more direct check - but you'll need to play with this on another system

(I'm assuming that an upgrade to 2014 for this feature is out of the question - your best bet is likely just looking for the bad row).

The problem is that you are trying to mix header information with data information in a single query.

Obviously, all your header columns will be strings. But not all your data columns will be strings, and SQL Server is unhappy when you mix data types this way.

What you are doing is equivalent to this:

select 'header1' as col1 -- string
union all
select 123.5 -- decimal

The above query produces the following error:

Error converting data type varchar to numeric.

...which makes sense, because you are trying to mix both a string (the header) with a decimal field.

So you have 2 options:

  1. Remove the header columns from your query, and deal with header information outside your query.
  2. Accept the fact that you'll need to convert the data type of every column to a string type. So when you have numeric data, you'll need to cast the column to varchar(n) explicitly.

In your case, it would mean adding the cast like this:

,'Active Energy kWh'

UNION ALL

,CAST(SUM(cast(invc.UNITS as Decimal (15,0))) AS VARCHAR(50)) -- Change 50 to appropriate value for your case

EDIT: Based on comment feedback, changed the cast to varchar to have an explicit length ( varchar(n) ) to avoid relying on the default length, which may or may not be long enough. OP knows the data, so OP needs to pick the right length.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM