Solved

Value of 0's not appearing.

Posted on 2016-09-23
9
49 Views
Last Modified: 2016-09-26
In my query, I use the following code:

 (SELECT  lpad(trunc(sum(prt.hours)*100),6,'0')  
       FROM prod.prtime prt
       WHERE( prt.tr_date >= add_months(trunc(sysdate, 'mm'),-12)
             AND   prt.tr_date < trunc(sysdate)  )
       and prt.pay_sum_grp in ('REG', 'REGH','OT','ED','OREN')
  --     and prt.employee = 19330
       and emp.company = prt.company (+)
       and emp.employee = prt.employee (+)
       group by prt.employee  ) HOURS_WORKED

For the most part, all the values come across. The issue I'm having is if the value is 0, a blank is being displayed instead of the 0.
Is there anything I can do to ensure that the 0's appear.
I've attached a file with the results I'm getting.
Blanks.docx
0
Comment
Question by:metalteck
  • 4
  • 3
  • 2
9 Comments
 
LVL 76

Accepted Solution

by:
slightwv (䄆 Netminder) earned 250 total points
ID: 41812382
It likely isn't a 0 issue.  More likely a null issue.

Try this:
lpad(nvl(trunc(sum(prt.hours)*100),'0'),6,'0')
0
 

Author Comment

by:metalteck
ID: 41812554
Slightwv, I tried that, but got the same result
0
 
LVL 76

Expert Comment

by:slightwv (䄆 Netminder)
ID: 41812574
The logic works:
SQL> select lpad(nvl(1*100,'0'),6,'0') from dual;
000100

SQL> select lpad(nvl(null*100,'0'),6,'0') from dual;
000000

SQL> select lpad(nvl(0*100,'0'),6,'0') from dual;
000000

Open in new window


Check what is coming back.  Query both and for one of the 'blank' ones, provide the dump output.
lpad(nvl(trunc(sum(prt.hours)*100),'0'),6,'0'), dump(lpad(nvl(trunc(sum(prt.hours)*100),'0'),6,'0'))
0
 
LVL 34

Expert Comment

by:Mark Geerlings
ID: 41812613
I agree with slightwv, it must be either a null value on a record, or a complete lack of matching records (which results in a null).  So you may need an nvl around this entire sub-query.
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

Author Comment

by:metalteck
ID: 41812674
Hi guys,

I queried a couple of the blanks using just the sub-w and I get no records returned.
I attached a screenshot of the results.  

The results are correct because these are new employees and would not have accumulated any working hours just yet.

Question is how can I have the value of 000 display for these new hires?
Blanks2.docx
0
 
LVL 76

Expert Comment

by:slightwv (䄆 Netminder)
ID: 41812686
If the SUM returns a null value, the code I posted will provide '000000'.  The blanks in the result set have to be nulls.  I really don't see a way around it.

NOW, if you are running code outside of that query and the query itself is returning an empty result set, you need to handle that in the app making the query.

You really can't make a query return a row when there are no rows found.  Well, you can but it isn't pretty.

You should handle the 'no rows found' in the app not in the SQL.
0
 

Author Comment

by:metalteck
ID: 41812703
That's exactly the problem now, since the records are null, nothing is being returned in the main query.  Since I'm using sql developer, how would I handle that in the app instead of the sql?

Would using a case statement be a not so pretty way of hard-coding 00000 whenever there is a null found?
0
 
LVL 34

Assisted Solution

by:Mark Geerlings
Mark Geerlings earned 250 total points
ID: 41812733
I just tested my earlier suggestion (to wrap the entire "(select...)" sub-query in an nvl, and that works.  Here are the two simple examples of that:

select (select 'x' from dual where 1 = 2) "Test" from dual;

T
-


select nvl((select 'x' from dual where 1 = 2),0) "Test" from dual;

T
-
0

The first query returns a blank, but the second returns a zero.
0
 
LVL 76

Expert Comment

by:slightwv (䄆 Netminder)
ID: 41813225
Wrapping the select will work but I think there is a bigger issue.

Based on the sample results you have and what you gave as the reason for the blank lines, you have to have one massive script that executes many different select statements.

If that is the case, my experience tells me that you probably have chosen the wrong solution to your overall requirements.

If the individual select statements are hitting the same tables over and over again, yes, you have chosen the wrong solution for a few reasons.

Post more about what you are hoping to accomplish and we can probably help get you to where you should be.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

707 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

12 Experts available now in Live!

Get 1:1 Help Now