Solved

How do you capture empty strings, null values and zero assigned records?

Posted on 2008-10-06
9
224 Views
Last Modified: 2010-03-19
Hi I am trying to write a script that simply will look into a table and count the number of records it has containing null values, empty string values, or zero assigned values.

How do I achieve this in one script?

I started with the below script and ended with this, but I still don't get the results returned correctly, in the sense that blank records are not being returned.
SELECT Count(BUSINESS.ACCT.ACCT_INTRL_ID) FROM BUSINESS.ACCT HAVING (((Count(BUSINESS.ACCT.ACCT_INTRL_ID)) Is Null));
 
SELECT Count(BUSINESS.ACCT.ACCT_INTRL_ID) FROM BUSINESS.ACCT HAVING (((Count(BUSINESS.ACCT.ACCT_INTRL_ID)) =''));

Open in new window

0
Comment
Question by:jperez484
  • 6
  • 3
9 Comments
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 22656315
COUNT and other aggregates typically optimize out NULL's.  Think this is what you are looking for.  You will have to see what the datatype of the column is as typically a string will not be 0 but could be '0' so I am assuming that in the code below.
SELECT SUM(CASE WHEN IsNull(BUSINESS.ACCT.ACCT_INTRL_ID,'') IN ('','0') THEN 1 ELSE 0 END) AS CountEmptyIntrlID
FROM BUSINESS.ACCT

Open in new window

0
 

Author Comment

by:jperez484
ID: 22656358
I see what your code is trying to do but I get the following error return:


Error starting at line 1 in command:
SELECT SUM(CASE WHEN IsNull(BUSINESS.ACCT.ACCT_INTRL_ID,'') IN ('','0') THEN 1 ELSE 0 END) AS CountEmptyIntrlID
FROM BUSINESS.ACCT
Error at Command Line:1 Column:21
Error report:
SQL Error: ORA-00904: "ISNULL": invalid identifier
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 22656386
Is this Oracle?  My syntax was for MS SQL Server.
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 59

Expert Comment

by:Kevin Cross
ID: 22656389
I am not the greatest at Oracle, but NVL should be the equivalent to T-SQL ISNULL:
SELECT SUM(CASE WHEN NVL(BUSINESS.ACCT.ACCT_INTRL_ID,'') IN ('','0') THEN 1 ELSE 0 END) AS CountEmptyIntrlID
FROM BUSINESS.ACCT

Open in new window

0
 

Author Comment

by:jperez484
ID: 22656434
I am getting no return for this script, so it must not be capturing the empty -blank records, as I know the data record has empty records in the table.  You mentioned you were handling the 0 as '0' can this be the effect? And yes, Its actually for both environment Oracle SQL developer and Microsoft, sorry for the confusion.


0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 22656505
What is the data type and some sample data of the records you are trying to capture in the count.  It could possibly be ' ' versus empty string or something else.  Please advise.
0
 
LVL 59

Accepted Solution

by:
Kevin Cross earned 500 total points
ID: 22656552
Think it is my syntax is still too much on the MS SQL side -- try this:
SELECT SUM(CASE WHEN NVL(TRIM(BUSINESS.ACCT.ACCT_INTRL_ID),'') = '' OR BUSINESS.ACCT.ACCT_INTRL_ID = '0' THEN 1 ELSE 0 END) AS CountEmptyIntrlID
FROM BUSINESS.ACCT

Open in new window

0
 

Author Comment

by:jperez484
ID: 22656676
Great!, this time it worked,  thank you for solving this dilemma for me!
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 22656686
No problem.

You are welcome.
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

Suggested Solutions

In this article I will describe the Copy Database Wizard 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.
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

830 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