Toad truncates trailing "0" decimal digits regardless of precision - oracle

I'm using Toad 12.6 for Oracle and when I try to pull a field whose data type is Decimal(5,2), Toad truncates the decimal for any trailing 0 decimal digits at the end of a number.
For example:
expected -> toad actual
5.40 -> 5.4 *incorrect
1.34 -> 1.34 *correct
10.24 -> 10.24 *correct
30.07 -> 30.07 *correct
There are a lot of threads about Toad truncating/rounding data when numbers are too large, and that's solved by telling it not to show large numbers with scientific notation.
Is there a setting I haven't found yet which will force Toad to display these trailing digits up to the precision? I would appreciate not having to manually cast a number field (to_char() or to_number()) every time I want to display uniform results.

As far as I can tell, there's no such setting in TOAD.
If it were SQL*Plus, you'd use SET command, e.g.
SQL> set numformat 999G990D00
SQL> with test (col) as
2 (select 5.40 from dual union all
3 select 1.34 from dual
4 )
5 select col from test;
COL
-----------
5,40
1,34
SQL>
TOAD is not SQL*Plus. However, if you put (literally) this into TOAD's Editor window and press F5, you'll get a desired result in its Output tab:
set numformat 999G990D00
with test (col) as
(select 5.40 from dual union all
select 1.34 from dual
)
select col from test;
The result will be
COL
-----------
5,40
1,34
2 rows selected.
Just to emphasize:
press F5, not F9, Shift+F9, Ctrl+Enter - nope; press F5
result isn't displayed in the Data Grid tab (but the Output tab)
There's a good chance that you won't like that approach. In that case, I believe that TO_CHAR is your savior.

Related

Checksum in Oracle PL/SQL

Do we have similar kind of checksum function (SQL) in oracle function. I would to store it in a table and use key/value for update statements. One of my current process contains 15 columns, need to check any change is there between source and destination, instead of checking one by one column whether change is happened or not, would like to have single column in table to helps whether change happened in 15 columns
Databases are good at keeping track of changes, that's what they are for.
Maybe the built-in ORA_ROWSCN is good enough for your change tracking? By default it tracks only the changes per block, but you can enable a table to track the change for each row with the - oddly named - clause ROWDEPENDENCIES:
CREATE TABLE t (
a NUMBER,
b NUMBER,
c NUMBER
) ROWDEPENDENCIES;
INSERT INTO t VALUES (1,1,1);
INSERT INTO t VALUES (2,2,2);
SELECT current_scn FROM v$database;
2380496
After a while:
UPDATE t SET b=20, c=20 WHERE a=2;
SELECT current_scn FROM v$database;
2380665
Now you can query the system change number SCN each row was changed:
SELECT ORA_ROWSCN AS scn, a,b,c FROM t;
SCN A B C
2380496 1 1 1
2380665 2 20 20
For the last 5 days, Oracle can translate a SCN to approximate real time (in Oracle 10 ± 5 minutes, from Oracle 11 ± 3 seconds):
SELECT ORA_ROWSCN AS SCN, SCN_TO_TIMESTAMP(ORA_ROWSCN) AS ts, a,b,c FROM t;
SCN TS A B C
2380496 2020-05-15 20:20:38 1 1 1
2380665 2020-05-15 20:23:06 2 20 20
It's documented in Oracles SQL Language Reference.

Sqlplus format 3 place number, i.e. 150 to 1.50 for spool output

I have a query in sqlplus that returns data to a csv file. One piece of data is an amount. It is displayed, for example, as 150. I need it to display 1.50. When I try using $9.99 or just 9.99 to try to format it, I get ###### in place of the amount.
I am new to sqlplus scripts and I need help. All other aspects of the query are working as expected. I have tried multiple masks but they just give me results such as 150.00 or 1500.00.
You need to convert the number into cents e.g. / 100 and to_char if you want the output to also include $'s
select to_char(150/100, '$9,999.99') from dual;
$1.50
select to_char(999999/100, '$9,999.99') from dual;
$9,999.99
note:
you will need to specify with enough precision:
select to_char(99999999/100, '$9,999.99') from dual;
##########
select to_char(99999999/100, '$999,999.99') from dual;
$999,999.99

Why Does Oracle 10g to_char(date time) Truncate Strings?

I got a bug report where Oracle 10g was truncating return values from to_char(datetime):
SQL> select to_char(systimestamp, '"day:"DD"hello"') from dual;
TO_CHAR(SYSTIMESTAMP,'"DAY:"DD"HE
---------------------------------
day:27hel
Notably, this does not appear to happen in Oracle 11g. My question is, why does it happen at all? Is there some configuration variable to set to tell to_char(datetime) to allocate a bigger buffer for its return value?
I'm not sure but it might be just displaying in SQL*Plus. Have you tried to run it in Toad? Or if you assign result to varchar2 in PL/SQL block and output result?
Here what I've found in SQL*Plus Reference for 10g:
The default width and format of unformatted DATE columns in SQL*Plus
is determined by the database NLS_DATE_FORMAT parameter. Otherwise,
the default format width is A9. See the FORMAT clause of the COLUMN
command for more information on formatting DATE columns.
Your values is trimmed to 9 characters which corresponds to default A9 format. I don't have same version and this behaviour is not reproducing in 11g so can you please check my theory?
I've got the same problem and I know the solution.
I use Release 11.2.0.4.0 but I beleave it is possible to repeat the situation in other versions. It somehow depends on client. (E.g. I cannot repeat it using SQL*Plus, only with PL/SQL Devepoper)
Try this:
select to_char(systimestamp, '"day:"DD"йцукенг OR any other UTF-encoded-something"') from dual
union all
select to_char(systimestamp, '"day:"DD"hello"') from dual;
You'll get the following result:
day:08йцукенг OR any other UTF-encoded-so
day:08hello
You can see the "mething" is lost. This is exactly 7 bytes exceeded because of 7 two-byte simbols "йцукенг". Oracle allocates buffer for the number of characters, not a number of required bytes.
The command
alter session set nls_length_semantics=byte/char
unfortunately does not affect this behavior.
So my solution is to cast a result as varchar2(enough_capacity)
select cast(to_char(systimestamp, '"day:"DD"йцукенг OR any other UTF-encoded-something"') as varchar(1000)) from dual
union all
select to_char(systimestamp, '"day:"DD"hello"') from dual
Explicit typecasting makes expression independent from client or configuration.
BTW, the same thing happens in all implicit to_char-conversions. E.g.
case [numeric_expression]
when 1 then '[unicode_containing_string]'
end
Result might be cutted.

Why does Oracle round up a number with less than 38 significant digits?

We have Oracle Server 10.2.
To test this, I have a very simple table.
CREATE TABLE MYSCHEMA.TESTNUMBER
(
TESTNUMBER NUMBER
)
When I try to insert 0.98692326671601283 the number gets rounded up.
INSERT INTO MYSCHEMA.TESTNUMBER (TESTNUMBER)
VALUES (0.98692326671601283);
The select returns:
select * from TESTNUMBER
0.986923266716013
It rounds up the last 3 numbers "283" to "3".
Even looking at it with TOAD UI and trying to enter it with TOAD, I get the same result.
Why? Is it possible to insert this number in an Oracle number without it getting rounded up?
I think you need to look into how your client program displays number values. An Oracle NUMBER should store that value with full precision; but the value may be rounded for display by the client.
For instance, using SQLPlus:
dev> create table dctest (x number);
Table created.
dev> insert into dctest VALUES (0.98692326671601283);
1 row created.
dev> select * from dctest;
X
----------
.986923267
dev> column x format 0.000000000000000000000000000
dev> /
X
------------------------------
0.986923266716012830000000000
As you can see, the default format shows only the first 9 significant digits. But when I explicitly change the column formatting (a client-side feature in SQLPlus), the full value inserted is displayed.

In PL/SQL, how do I use a varaible in a SELECT *without* using an INTO?

I'm experimenting with queries in an Oracle database (Using Oracle SQl Developer and PL/SQL Developer)
If I run a simple query: (SELECT * FROM myTable WHERE id = 1234) the results display in a nice grid in a lower pane of the SQL tool.
Now, how do I rewrite that query, using a variable for the 1234, And STILL have the results spill out into the results pane. Everything I've tried either won't compile, or requires me to do a SELECT...INTO and then manually output the results.
I just want to do something along the lines of this, and have it work:
DECLARE p0 = 1234;
SELECT * FROM myTable WHERE id = p0;
UPDATE:
In the actual query I'm working on, the variables will be more like:
DECLARE p0 = to_date('1/15/2014 7:11:05 AM','MM/DD/YYYY HH:MI:SS PM');
p1 = p0 + .0007; -- one minute later.
So being able to write that in code is important.
In Toad (or sqlplus or SQL Developer) you would do:
define x='20140820';
select to_date(&x, 'YYYYMMDD') from dual;
And run as a Script (important).
In Toad, the Output grid would be:
old: select to_date(&x, 'YYYYMMDD') from dual
new: select to_date(20140820, 'YYYYMMDD') from dual
TO_DATE(20140820,'YYYYMMDD')
----------------------------
20-AUG-2014
1 row selected.
And Grid1 would just show the results in table grid format.
Note that you can suppress the old/new in output by doing:
set verify off
at top of script.
One thing you can try is to create a bind variable named cur, for example, of type REFCURSOR, use OPEN :cur FOR SELECT ... in your PL/SQL block and then PRINT the cursor out. Here's an example with a very simple query:
VARIABLE cur REFCURSOR
BEGIN
OPEN :cur FOR SELECT * FROM DUAL;
END;
/
PRINT cur
This works in SQL*Plus and SQL Developer, although in the latter it will only work if you use the 'Run Script (F5)' button, not the 'Run Statement (Ctrl+Enter)' button. Also, you don't get a table with the results in, just the query output in preformatted text. Also, it won't necessarily work in other tools, as VARIABLE and PRINT are not part of SQL nor PL/SQL - SQL*Plus and SQL Developer both understand them and can interpret them.
In PL/SQL Developer you can use a Test Window for this kind of thing. For example, enter into a Test Window a PL/SQL block such as
BEGIN
OPEN :cur FOR SELECT * FROM DUAL;
END;
Then, either add the variable cur of type 'Cursor' to the variables table at bottom of the window, (or choose Scan Variables from the context menu. Note that PL/SQL Developer won't necessarily get the type right; you may well still have to change the type. Once you run the block, the results from the cursor can be obtained in a separate window by clicking the '...' button in the row in the variables table for cur.
As a minor variation on Luke's excellent answer, you don't even need a ref cursor if you just want to display a variable's value. Put this in a Test window:
declare
p0 date := to_date('1/15/2014 7:11:05 AM', 'MM/DD/YYYY HH:MI:SS PM');
begin
:p1 := p0 + .0007;
end;
then set up the p1 bind variable in the lower panel. Executing it will display the value.

Resources