Solved

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

Posted on 2008-10-06
9
215 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
 
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
In this article I will describe the Backup & Restore 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.
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.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

743 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