Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 238
  • Last Modified:

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

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
jperez484
Asked:
jperez484
  • 6
  • 3
1 Solution
 
Kevin CrossChief Technology OfficerCommented:
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
 
jperez484Author Commented:
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
 
Kevin CrossChief Technology OfficerCommented:
Is this Oracle?  My syntax was for MS SQL Server.
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
Kevin CrossChief Technology OfficerCommented:
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
 
jperez484Author Commented:
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
 
Kevin CrossChief Technology OfficerCommented:
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
 
Kevin CrossChief Technology OfficerCommented:
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
 
jperez484Author Commented:
Great!, this time it worked,  thank you for solving this dilemma for me!
0
 
Kevin CrossChief Technology OfficerCommented:
No problem.

You are welcome.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 6
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now