Solved

Money column shows 0 in query, filters as blank?

Posted on 2015-01-14
10
170 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
  • 5
  • 5
10 Comments
 
LVL 69

Expert Comment

by:ScottPletcher
Comment Utility
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
Comment Utility
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 69

Expert Comment

by:ScottPletcher
Comment Utility
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
 

Author Comment

by:jay-are
Comment Utility
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 69

Expert Comment

by:ScottPletcher
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:jay-are
Comment Utility
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 69

Expert Comment

by:ScottPletcher
Comment Utility
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
Comment Utility
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 69

Accepted Solution

by:
ScottPletcher earned 500 total points
Comment Utility
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
Comment Utility
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

Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

Join & Write a Comment

In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
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.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

771 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