Oracle display value before decimal for 0's [duplicate] - oracle

This question already has answers here:
How to show the zero in 0.24 [duplicate]
(2 answers)
Fetching value from a number column removes 0 before decimal
(1 answer)
Closed 5 months ago.
For exmaple:
select 'A='||(0.22*0.11)||'' as A from dual;
it does return A=.0242
expected A=0.0242enter code here

If you use to_char function with desired format mask, then you get
SQL> select 'A='|| to_char(0.22*0.11, 'fm999G990D0000')||'' as A from dual;
A
---------------
A=0,0242
SQL>
As of a "generic" format model: you can't "dynamically" set it, but - if you use 9 instead of 0 after decimal point, you might get what you wanted:
SQL> select 'A='|| to_char(88.223*99.112, 'fm999G990D9999999999')||'' as A from dual;
A
---------------------
A=8.743,957976
SQL>
Benefit: it returns result you want
Drawback: how many 9s will you have to put in there? You can't tell - set it to the "worst" case you expect (such as 10 of them in my example)

Related

What happens? <= on two varchars? [duplicate]

This question already has answers here:
SQL string comparison, greater than and less than operators
(4 answers)
Closed 5 years ago.
In my Oracle db, I noticed some strange behavior when doing a <= comparison on two numbers. And I found the problem. The .net code was passing in one of the values as a DbParameter of type string. And the Oracle column type of the other number was a varchar. So essentially what was happening was a <= on two numbers that were actually varchars.
Can someone explain why the following statement is true in PLSQL?
'10000001' <= '50000'
Becuase-
SQL> select ascii('10000001') from dual;
ASCII('10000001')
-----------------
49
SQL> select ascii('50000') from dual;
ASCII('50000')
--------------
53
SQL> select 'true' from dual where ascii('10000001') < ascii('50000');
'TRU
----
true

Using Dates in PL/SQL [duplicate]

This question already has answers here:
Working with dates in Oracle SQL
(3 answers)
Closed 6 years ago.
I am very new to using PL/SQL and I have created a procedure but I cannot figure out the correct syntax in order to get the current month and year's data and another cursor to get the data from exactly 1 year prior:
create or replace procedure data(acc integer, month integer, year integer)
as
Cursor c1 is
select usage
from bill
where account =acc_num and to_char(BILL_DATE, 'MM-YYYY') = 'month-year';
Cursor c3 is
select usage
from bill
where account =acc_num and
to_char(BILL_DATE, 'MM-YYYY') = 'month-year' - 1;
** I do understand this is only part of the code, but I believe my logic is almost complete for finding the data I want. Using PLSQL
I think you are looking for something like this:
select usage
from bill
where account = in_account and
extract(year from bill_date) = in_year and
extract(month from bill_date) = in_month;
If you want to compare the year and month (which are passed in as integers), just extract those attributes from the date.
If you are learning PL/SQL, learn to name your parameters and arguments so you can distinguish them from columns:
create or replace procedure data (
in_account integer,
in_month integer,
in_year integer
) as
begin
. . .
(And "data" is a very curious name for a stored procedure. I would expect a verb in the name.)

Oracle plsql how to check if number has decimal points

Just started working with oracle using toad ide. trying to format the numbers from a table in specific format. the numbers come in from a variable in the table and I want to display the whole numbers as whole numbers and display floats as floats. So far, I can use trim(TO_CHAR (width,'999.999')) to display all numbers with decimal points.
For example: 123.5 will be displayed as 123.500 and 100 will be displayed as 100.000.
What I want to do is display for eg: 100 as 100.
Hope this is clear and I get a solution soon.
I'm using MOD for determining decimals.
select test_value, (case when mod(test_value,1) != 0 then 'DECIMAL' else 'NODECIMAL' END) IS_DECIMAL
from (select 1.5 test_value from dual
union all
select 100 test_value from dual) test_table
If your problem is about the way Toad shows numbers, you can follow the hints in the comments.
If the problem is about the way Oracle shows numbers, converting them to strings, maybe this can help:
SQL> select to_char(1.5, 'TM9') as num from dual union all
2 select to_char(100, 'TM9') from dual;
NUM
----------------------------------------------------------------
1,5
100
You find much more in the documentation
If you need a way to check whether a number has a decimal part or not, you can simply try:
SQL> with numbers(num) as (
2 select 1.5 from dual union all
3 select 100 from dual
4 )
5 select case
6 when floor(num) = num
7 then to_char(num, 'FM999999') || ' has not a decimal part'
8 else
9 to_char(num, 'FM9999D000') || ' has a decimal part'
10 end as checkString
11 from numbers;
CHECKSTRING
------------------------------
1,500 has a decimal part
100 has not a decimal part

Why oracle doesn't show the integer part of a decimal

when I execute the next SQL:
select 1/3 from dual;
the result is ,333333333
but the result that I expected was 0,333333333
Why doesn't oracle show the zero ? is there any parameter that can affect it?
Thanks
Edit: I checked this behaviour from an aplication that uses the oo4o (oracle object for ole), and confirmed with the sqlDeveloper (when I execute the sql with F5, not with ctrl+intro). The real problem is with the oo4o Dynaset object: when I try to sum the value of two fields, what I get is a concatenation, not a sum: ,3 + ,2 = ,3,2.
What I want to know is if there is some parameter or configuration that makes the oracle engine return the number without the zero, so I can turn on|off in order to return de zero as integer part.
SQL*Plus will show that by default (using your territory's decimal separator):
SQL> select 1/3 from dual;
1/3
----------
.333333333
You could use set numformat to change the behaviour:
SQL> set numformat "0D9999"
SQL> select 1/3 from dual;
1/3
-------
0.3333
... where the D represents the decimal separator. Or you could use column formatting, with a column alias:
SQL> set numformat ""
SQL> column answer format 0.000
SQL> select 1/3 as answer from dual;
ANSWER
------
0.333
Other clients have different ways of controlling the default output; SQL Developer behaves much the same, but PL/SQL Developer, Toad etc. might not.
Or you can format the number as part of the query, which isn't client-dpendent:
SQL> select to_char(1/3, '9990D99999') from dual;
TO_CHAR(1/3
-----------
0.33333
You need to provide enough digits for the integer part of whatever you're calculating though. Anything less than zero is simple, but if there are too many digits before the decimal separator then it won't display at all:
SQL> select to_char(100000/3, '9990D99999') from dual;
TO_CHAR(100
-----------
###########

Oracle cursor removes leading zero

I have a cursor which selects date from column with NUMBER type containg floating point numbers. Numbers like 4,3433 are returned properly while numbers smaller then 1 have removed leading zero.
For example number 0,4513 is returned as ,4513.
When I execute select used in the cursor on the database, numbers are formatted properly, with leading zeros.
This is how I loop over the recors returned by the cursor:
FOR c_data IN cursor_name(p_date) LOOP
...
END LOOP;
Any ideas why it works that way?
Thank you in advance.
You're confusing number format and number value.
The two strings 0.123 and .123, when read as a number, are mathematically equals. They represent the same number. In Oracle the true number representation is never displayed directly, we always convert a number to a character to display it, either implicitly or explicitly with a function.
You assume that a number between 0 and 1 should be represented with a leading 0, but this is not true by default, it depends on how you ask this number to be displayed. If you don't want unexpected outcome, you have to be explicit when displaying numbers/dates, for example:
to_char(your_number, '9990.99');
It's the default number formatting that Oracle provides.
If you want to specify something custom, you shall use TO_CHAR function (either in SQL query or PL/SQL code inside the loop).
Here is how it works:
SQL>
SQL> WITH aa AS (
2 select 1.3232 NUM from dual UNION ALL
3 select 1.3232 NUM from dual UNION ALL
4 select 332.323 NUM from dual UNION ALL
5 select 0.3232 NUM from dual
6 )
7 select NUM, to_char(NUM, 'FM999990D9999999') FORMATTED from aa
8 /
NUM FORMATTED
---------- ---------------
1.3232 1.3232
1.3232 1.3232
332.323 332.323
.3232 0.3232
SQL>
In this example, 'FM' - suppresses extra blanks, '0' indicates number digit including leading/trailing zeros, and '9' indicates digit suppressing leading/trailing zeros.
You can find many examples here:
http://docs.oracle.com/cd/B19306_01/server.102/b14200/sql_elements004.htm#i34570

Resources