Number of decimal digits in Oracle SQL Developer query result sheet - oracle

The Date format can be customized in Oracle SQL Developer, how Decimal format can be specified? By that I mean number of decimal digits.
In Tools>Preferences>Database>NLS we only have decimal separator, nothing else.
Thank you in advance.

There is no option in Preferences to my knowledge. You can use to_char function to display the decimal digits.
An example:
Try this in HR schema:
select to_char(salary,'999999.000'), employee_id from employees;

Related

Truncating Numbers with Oracle 12c

This is not about truncating to decimal places. Rather, truncating whole numbers with Oracle 12c.
select CAST('123456789' AS NUMBER(4)) from DUAL;
It would be great if this returned '1234' instead of throwing an exception.
As suggested by Justin, it is not sensible to do so. But it can be done like this.
select cast(substr('123456789',1,4) as integer) from dual;
But this will not work in following scenarios.
You have 0 before the number.
You have a decimal digit before
4th digit (ex 123.4)

How many bytes takes DateTime format in oracle SQL?

Umm, Hello, can someone tell me how many bytes takes Date Time format in Oracle SQL pls ?
I tryed to find it with google, but i couldnt find it.
Thank for answer.
You can use DUMP function to retrive information about expression value, like this SELECT DUMP(SYSDATE,10) FROM dual
and it says Typ=13 Len=8: 223,7,4,23,14,17,41,0, so 8 bytes.
From oracle docs
http://docs.oracle.com/cd/B28359_01/server.111/b28286/functions048.htm#SQLRF00635
DUMP returns a VARCHAR2 value containing the datatype code, length in
bytes, and internal representation of expr. The returned result is
always in the database character set.
Aslo, there are two different raw forms for DATEs in Oracle:
https://community.oracle.com/thread/2257401
Type 12 (7 bytes) is used for DATE columns
Type 13 (8 bytes)
is used for other DATE expressions, including DATE literals and results for date arithmetic and functions.
The size of a date time format is fixed at 7 bytes, see Oracle's documentation or alternatively run this:
select vsize(sysdate) from dual
or this:
create table test (dt date);
select data_length from user_tab_columns where table_name = 'ZTEST';

how to insert iso-8601 type date into oracle database

I am using Oracle 11g and trying to figure out how to insert this date into my table. The date seems like it is ISO-8601, but the 7 zeros are confusing me.
Insert into myTestTable (myDate) values ('2013-01-22T00:00:00.0000000-05:00');
I have tried to format the date with no luck. The error I am getting is ORA-01861 literal does not match format string.
First of all, the column must be a TIMESTAMP WITH TIME ZONE type to hold the value you're trying to insert. An Oracle DATE is accurate to seconds only, and it doesn't hold a time zone.
The seven zeros are the fractional seconds. The default precision for a TIMESTAMP WITH TIME ZONE happens to be seven decimal places. To specify three decimal places for seconds, define the column as TIMESTAMP(3) WITH LOCAL TIME ZONE.
The actual number of decimal places returned by SYSTIMESTAMP, which is the current timestamp at the server, depends on the operating system. My local Windows 7 Oracle returns three significant decimal places, while the Solaris OS at one of my clients returns six significant decimal places.
As for inserting the value, if you do something like this...
insert into myTestTable (myTS) values ('2013-01-22T00:00:00.0000000-05:00');
... Oracle will try to convert the timestamp using its current NLS_TIMESTAMP_TZ_FORMAT setting. You can query the setting like this:
SELECT *
FROM NLS_Session_Parameters
WHERE Parameter = 'NLS_TIMESTAMP_TZ_FORMAT';
PARAMETER VALUE
----------------------- ----------------------------
NLS_TIMESTAMP_TZ_FORMAT DD-MON-RR HH.MI.SSXFF AM TZR
The result I got is the "factory default". Yours is probably the same, and you can see it doesn't match the format you've given, hence the conversion fails.
As another answer correctly notes, you can use the TO_TIMESTAMP_TZ function and a format string to convert the string to a timestamp. You can also use an ANSI timestamp literal:
insert into myTestTable (myTS)
values (TIMESTAMP '2013-01-22T00:00:00.0000000-05:00');
Oracle documents timestamp literals here. The link covers literals for all types, so you'll need to scroll about two-thirds of the way down the screen (or do a find) to get to the timestamp literals.
Here is the answer.
insert into myTestTable (myDate) values
(to_timestamp_tz('2013-01-22T00:00:00.0000000-05:00',
'YYYY-MM-DD"t"HH24:MI:SS.FF7TZR'));

Need to display decimal values in oracle

I need to display the 3 decimal values if the number having decimal value,
for suppose the number does not having decimal then will display zero's after decimal
I tried with TO_CHAR function but when i am converting to_char into TO_NUMBER it's displaying number with rounded values
Your question is a bit vague, but I guess you need something like
TO_CHAR(n, '999G990D000')
to show the zeros.
If you have privileged to change the SCHEMA, then you can do it this way:
ALTER TABLE TABLENAME Modify Attrib Number(10,3);

Oracle to_number function parameters

I'm having trouble with TO_NUMBER function second and third parameters. Does one of them depend on the other one? How does nls_params parameter work? I can't understand how the the result of the query
SELECT TO_NUMBER('17.000,23',
'999G999D99',
'nls_numeric_characters='',.'' ')
REFORMATTED_NUMBER
FROM DUAL;
can be 17000.23. Could somebody please explain the process of the above conversion.
P.S. The above query is taken from an Oracle Database SQL Expert Certificate preparation book.
you are telling the TO_NUMBER function that,
the two characters ,. in nls_numeric_characters represent the decimal and thousand seperator
G (thousands seperator) = .
D (decimal seperator) = ,
so it sees the number as seventeen thousand point twenty three.
see: http://download.oracle.com/docs/cd/B13789_01/olap.101/b10339/x_stddev022.htm#i78653
Now, I'll answer my own question. While using TO_NUMBER function I missed the important point that, whatever I get from TO_NUMBER function is going to be a number. And a number does not include anything else than decimal point and E scientific notation. So 17,788.99 is not actually a number but is rather the string representation of 17788.99.
If we try to subtract 500 from 17,788.99 we'll fail.(Well, Oracle implicitly converts numeric strings to numbers and vice-versa, but principally we can't perform arithmetic operations between strings and numbers). I'm sure that TO_NUMBER function is almost never used to select a column value. It's rather used to be able to make arithmetic operations. Instead, we use TO_CHAR to show a column value or any numeric expression in a neat, easy to read format. The fomat models and nls_params are not only for TO_NUMBER function, but for TO_CHAR as well.

Resources