Solved

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

Posted on 2008-10-06
9
222 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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
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

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

Suggested Solutions

Title # Comments Views Activity
How can i use WITH CTE for checking exist value? 3 32
This query failed in sql 2014 5 30
T-SQL: "HAVING CASE" Clause 1 24
Sql Query 6 65
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
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.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
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…

776 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