Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Money column shows 0 in query, filters as blank?

Posted on 2015-01-14
10
Medium Priority
?
257 Views
Last Modified: 2015-01-15
Hello Experts,

I'm having a strange problem with a table.  I'm importing XML data into a loading table then inserting/updating to a final table.  There are 20k+ rows in the final table, and 499 of them contain "blank" cost fields.  Here is the query I use to determine this:

select cost from mytable where cost = ''

Open in new window


This shows data like this:

Cost
0.00
0.00
0.00
etc.

The cost field is a money datatype.  The xml source data has a 0 for this field.  During the import I use a data conversion task to convert everything from the xml file to "String [DT_STR]".  The load table has cost setup as a varchar datatype.  That's all I do as far as data conversion.  The cost column does not have a default binding set or anything like that.  However when queried it returns these 0.00 rows as blanks.  They aren't being counted as NULL, just blank.  This causes a big headache when trying to report off these rows and using functions like SUM, AVG, etc...

Any ideas why these specific rows are coming back as "blank", even though they show 0.00, and how to fix them so they return as 0?
0
Comment
Question by:jay-are
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
10 Comments
 
LVL 70

Expert Comment

by:Scott Pletcher
ID: 40549629
It's not "showing" or stored as blank.  SQL is converting the empty string/"blank" to 0.00 when it converts it to the money data type.

SELECT CAST('' as money)

If they show as blank coming out of the table, it's because 0.00 is being edited to blank.

If you want 0.00 to be NULL, run an UPDATE on the table changing 0.00 to NULL.
0
 

Author Comment

by:jay-are
ID: 40549670
Since I'm querying a money data type column that contains a blank it shows 0.00 in the query results?  Should I set the default binding for the cost column to 0?  Would that avoid future confusion on my part?  :)
0
 
LVL 70

Expert Comment

by:Scott Pletcher
ID: 40549704
The money data type does not "contain" a blank.

When you compare a blank to it, SQL converts the blank to 0.00, then does the comparison.

Again, run the statement below and you'll see that blank can't  be stored in a money data type:

SELECT CAST('' as money)
0
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

 

Author Comment

by:jay-are
ID: 40549756
Ok so there is actually a Null value for Cost that is being stored as 0.00 since I have the data type for that column set as money?
0
 
LVL 70

Expert Comment

by:Scott Pletcher
ID: 40549957
Hmm, probably not NULL.  You probably loaded an empty string '', which SQL converted to 0.00.

If you need NULL instead of zero, change that in your load:

INSERT INTO ... ( ..., Cost, ...
SELECT ..., NULLIF(Cost_In, ''), ...

Or, if you want all zeros to always be NULLs, UPDATE the table accordingly:

UPDATE table_name
SET Cost = NULL
WHERE Cost = 0.00
0
 

Author Comment

by:jay-are
ID: 40551375
I'm not sure why it would convert the source data (which shows 0) to an empty string, but I do have a string conversion taking place during my initial import.  

Assuming that an empty string was loaded into this table, how do I update it so these rows aren't returned when I query where cost = ''?  I don't want that condition to exist.  I tried updating the table and setting cost = 0 where cost = ''.  It says it updated the rows, but the original query still returns the same rows.
0
 
LVL 70

Expert Comment

by:Scott Pletcher
ID: 40551453
If the data type is money, an empty string CANNOT be loaded into that column, period.  Instead, the space is converted to 0 because of data type precedence.
0
 

Author Comment

by:jay-are
ID: 40551574
Right, it tried to insert an empty string into the money field and it was converted to a 0.  Got it.  Why does the query:
 
select cost from mytable where cost = ''

Open in new window


return data then?
0
 
LVL 70

Accepted Solution

by:
Scott Pletcher earned 2000 total points
ID: 40551652
Because SQL converts the '' to a 0, then compares.

In more detail:
SQL follows rules of data type precedence.  SQL can only compare compatible data types.  If you compare two data types that are not compatible -- such as Money and string -- SQL must be able make them compatible before comparing them.  Since money has a higher precedence, SQL implicitly and automatically first converts the string to money, then does the comparison.

Character data will always be converted before being compared to numeric data.  SQL will attempt the conversion even if it fails, because SQL must make the data types compatible before it can compare them:

SELECT CASE WHEN '9' > 11 THEN 'True' ELSE 'False' END --F
SELECT CASE WHEN '9' > '11' THEN 'True' ELSE 'False' END --T
SELECT CASE WHEN '9A' > '11' THEN 'True' ELSE 'False' END --T
SELECT CASE WHEN '9A' > 11 THEN 'True' ELSE 'False' END --Conversion error!
0
 

Author Comment

by:jay-are
ID: 40551743
I appreciate you taking the time to explain this.  I realize I'm hard headed!  I guess I need to work on the import of the xml to ensure there aren't any empty strings before inserting into this table.
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
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.
Viewers will learn how the fundamental information of how to create a table.

721 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