Skip to main content

SQL Server, Subqueries and data types

Today I had to deal with an issue on SQL Server 2005 that I thought might be worth sharing. Maybe someone else runs into a similar issue and can't figure out why SQL Server is playing stupid.

I was trying to run a query similar to the one below, it was generated by a reporting tool:

select sum(a11.COGS_ORDERED), 
sum(a11.LINE_RECEIVED)
from 
(SELECT 
ID
, COGS_ORDERED
, QTY_SHORTED
, 'LINE_RECEIVED' =
 CASE
   WHEN QTY_SHORTED = 0 THEN 1
   ELSE 0
 END
FROM myTable a11)

SQL Server always returned the error message "Arithmetic overflow error converting expression to data type int." . I figured out that the issue has to do with my case statement so I tested to run a sum right on the case statement and it worked fine:


SELECT 
SUM( CASE
   WHEN QTY_SHORTED = 0 THEN 1
   ELSE 0
 END)
FROM myTable a11


After doing some research I found out that SQL Server stores the result of the subquery as int and is not able to convert it to bigint when doing the SUM. Forcing a convert already in the case statement fixes the issue:

select sum(a11.COGS_ORDERED), 
sum(a11.LINE_RECEIVED)
from 
(SELECT 
ID
, COGS_ORDERED
, QTY_SHORTED
, 'LINE_RECEIVED' =
 CASE
   WHEN QTY_SHORTED = 0 THEN CONVERT(BIGINT,1)
   ELSE CONVERT(BIGINT,0)
 END
FROM myTable a11)

I think a database management system should be smart enough to do this conversion by itself.

Comments

Pravesh Singh said…
Very informative post, you explain very well on SubQuery in SQL server. I've some good articles too which I was found during searching this topic over internet. Here I'm sharing that post links........
http://www.mindstick.com/Blog/202/Sub%20query%20in%20sqlserver

http://www.dbtalks.com/UploadFile/rohatash123/495/

I'm very thankful for your nice post.

Popular posts from this blog

Pentaho Data Integration - Multi-part Form submission with file upload using the User Defined Java Class Step

I recently needed to use Pentaho Data Integration (PDI) to send a file to a server for processing using HTTP Post. I spent several hours trying to use the existing steps HTTP Post, HTTP Client & Rest Client but I couldn't get it to work. After some more research I came across the issue PDI-10120 - Support for Multi-part Form Submittal In Web Service Steps  and I thought I was out of luck. I previously wrote a small Java client for a similar use case and remembered the PDI has a step called User Defined Java Class  (UDJC). After reading this great tutorial I created the following basic transaction. I have a dataset with the URL and the full file path and use the UDJC to make the HTTP call. HTTP Post using User Defined Java Class The Java class handles the actual HTTP Post. It uses 2 input variables, the URL (url) which is used for the call and the file name (longFileName). The HTTP call then contains the file (line 30) and the file name (line 31). I included some basi

Products you don't expect to be 'Made in China' - Del Monte fruit cups

Since I moved to Canada back in March I have started to realize how many products are actually made in China. Back in Germany you could also buy lots of stuff from China but you mostly had the choice between German or Europe products and Chinese products. When I went to Food Basics in Oakville a couple weeks ago to get some apples I stood in front of a huge tray of Chinese apples! Aren't there enough apples in Ontario, Canada or the US? Even Mexico would probably be closer than China. Another day my wife bought Del Monte fruit cups in the grocery store. I checked the label when I was going to eat it and i decided to leave it in the fridge. First of all it is 'Made in China' (again I guess no other country in this world has fruit) and second it contains artificial flavor. How bad must the fruit inside be that you need artificial flavor (and does anybody in China controls how it is made)? For my part I'll check the labels more closely whenever I buy any kind of product

Open Source tool for Data cleansing and Master Data Management

Last weekend SQL Power released an improved version of SQL Power DQguru (formerly known as SQL Power MatchMaker), one of the few open source tools for data cleansing and master data management (MDM) available. Version 0.96 brings a new feature that allows you to run SQL Power DQguru from command line. This allows you to integrate it into batch scripts and your ETL jobs. As a BI consultant for SQL Power I have used SQL Power DQguru in different projects and it has made my job a lot easier. Some of the features I like the most are: Easy connection to any database with JDBC drivers, incl. SQL Server, Oracle, MySQL, Postgres Lets you create complex merge rules so your dependent data will always be updated when you merge records. You can combine over 25 steps to find possible duplicate data with a match rule, for example: Word Count Regular Expressions Substrings Retain certain characters Translate Words, you can create your own translation rules. You can preview how your data