Solved

Money column shows 0 in query, filters as blank?

Posted on 2015-01-14
10
179 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
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 69

Expert Comment

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

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 69

Expert Comment

by:ScottPletcher
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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 

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 69

Expert Comment

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

Accepted Solution

by:
ScottPletcher earned 500 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
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 extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
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.

929 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