How to change Number format in oracle? - oracle

I have a requirement to globalize the application based on culture specified in config file.
I have Amounts and date values .I decided to convert them in query itself.Like below
For Spanish Chile Es-CL:
for Date format is "dd-mm-yyyy" I will use Select TO_CHAR(busdate,'dd-mm-yyyy') from itemTable.
Similarly i want to use same for the amounts Select to_char(Amount,'99.999.99,00') from table1.
But its in vain.
Please suggest me the right way to achive my requirement.
These amounts and date formats can be available.but need to know how to convert them.
I thought to use in On_Data_bound event for datagrids ,but still this will be a performance issue.

You would use the NLS_NUMERIC_CHARACTERS session variable, along with globalized format model:
SQL> alter session set NLS_NUMERIC_CHARACTERS='.,';
Session altered
SQL> select to_char(123456789.01, 'fm999G999G990D00') from dual;
TO_CHAR(123456789.01,
---------------------
123,456,789.01
SQL> alter session set NLS_NUMERIC_CHARACTERS=', ';
Session altered
SQL> select to_char(123456789.01, 'fm999G999G990D00') from dual;
TO_CHAR(123456789.01,
---------------------
123 456 789,01

Related

Oracle- save dates with correct centuryinfo

I am trying to insert nvarchar2 values into my oracle Date field. When my source nvarchar2 value is '20701130', I want to save it as '30/11/2070' in my oracle column. And, if the source value is '21150529', I want to save it as '29/05/2115'.
I tried different formatting options to achieve this, but the year is always getting saved as two digits and is not indicating the correct century.
One of my Queries:
INSERT INTO MY_TABLE (MY_DATE_FIELD)
VALUES(TO_DATE('21061030', 'RRRR-MM-DD'));
Now, when I select after the above query, I get the result as 30/10/06 which is not we want, it should be '30/10/2106'.
You can go about it in many ways:
You alter the session date format itself to a format you want to achieve like :
ALTER SESSION SET NLS_DATE_FORMAT = 'RRRR-MM-DD';
Another approach is once you have achieved a date from a string passed using to_date then you can again add to_char to display the date in a format you want.
To_date will convert your string to a date using the format you have provided but while selecting it will still display the date as per the format provided in the session that's why we use To_char to change it to a format we want to display it into.
select to_char(TO_DATE('21061030', 'RRRR-MM-DD'),'RRRR-MM-DD') "DATE" from dual;
Thanks for the help.
I have pasted my SQL below if anyone is interested.
-- To insert into table
UPDATE MY_TABLE SET MY_DATE_FLD = TO_CHAR(TO_DATE('21061030', 'RRRR-MM-DD'),'DD-MM-YYYY') ;
-- To read from the table, this gave me the result 30/01/2106
select TO_CHAR(DATE_FLD,'DD/MM/YYY') AS START_DATE from MY_TABLE;
Appreciate the help.
Kiran.

Oracle TO_DATE with only time input will add date component based on what logic?

Running this code in Oracle 11/12:
select to_date('101200', 'hh24miss') from dual
will return a DATE component that Oracle automatically adds based on what logic?
Eg:
select to_char(to_date('101200', 'hh24miss'), 'yyyymmdd') from dual
returns
20160701
We see the added date component is always set to the first day of the current month. Where does this logic come from?
Thanks in advance
A value of date data type always has date and time components. if you specify only time portion of the datetime value as you did, the date portion defaults to the first day of the current month.
Here is one of the places (7th paragraph) in the Oracle documentation where this behavior is documented.
There is also undocumented TIME literal and TIME data type (needs to be enabled via 10407 (datetime TIME datatype creation) event) if you need to use and store just time, without date part.
Here is a small demonstration of using time literal and time data type. But again it's undocumented and unsupported feature.
SQL> select time '11:32:00' as res
2 from dual;
res
------------------------
11.32.00.000000000 AM
You can use time literal without enabling 10407 event, but in order to be able to define a column of time data type the 10407 event needs to be enabled:
SQL> create table time_table(time_col time);
create table time_table(time_col time)
*
ERROR at line 1:
ORA-00902: invalid datatype
-- enable 10407 event
SQL> alter session set events '10407 trace name context forever, level 1';
Session altered.
Now we can create a table with a column of time data type:
SQL> create table time_table(time_col time);
Table created.
SQL> insert into time_table(time_col)
2 values(time '11:34:00');
1 row created.
SQL> select * from time_table;
TIME_COL
---------------
11.34.00 AM
SQL> alter session set events '10407 trace name context off';
Session altered.

oracle date format issue

We use Oracle 10.2.0.4.0 database, oracle form builder and report builder for creating forms and reports.
Now the problem is in our production database nls_date_format is dd-mon-rr format. When developer create form in developer suit they give dd-mm-rr format at form level and when data stored in table that date format is dd-mm-rr.
Now when developer run form or report within form builder it gives dd-mm-rr format.but when same form or report run from application server side it gives junk characters in month.date and year print same as date format only month display in junk characters.
Hope you all guide well.
There are two issues.
when data stored in table that date format is dd-mm-rr.
This is completely wrong. Oracle doesn't store the date in the format you see, what you see is for display. Oracle stores DATE in an internal proprietary format in 7 bytes with each byte representing different elements of the DATE.
Byte Description
---- -------------------------------------------------
1 Century value but before storing it add 100 to it
2 Year and 100 is added to it before storing
3 Month
4 Day of the month
5 Hours but add 1 before storing it
6 Minutes but add 1 before storing it
7 Seconds but add 1 before storing it
Do not depend on the locale_specific NLS_DATE_FORMAT. Always use:
TO_CHAR to display the date in your desired format
TO_DATE to explicitly convert the string into date.
Remember, TO_DATE is NLS dependent.
If you only have a date element, and if you do not care about the time element, then better use ANSI Date literal which follows a fixed format 'YYYY-MM-DD'.
only month display in junk characters
This is again because you are depending on the NLS_DATE_LANGUAGE. As I said, you should avoid depending on the locale-specific client settings. Explicitly mention the NLS_DATE_LANGUAGE or use ANSI Date literal if you are not concerned about the time element.
For example,
SQL> SELECT TO_CHAR(SYSDATE, 'DD-MON-RR') dt FROM DUAL;
DT
---------
26-OCT-15
SQL> alter session set nls_date_language='french';
Session altered.
SQL> SELECT TO_CHAR(SYSDATE, 'DD-MON-RR') dt FROM DUAL;
DT
-----------
26-OCT. -15
So, what happened above? for a person using FRENCH nls_date_language, the MONTH is showing junk value. Let's make it NLS independent by explicitly mentioning the nls_date_language.
SQL> SELECT TO_CHAR(SYSDATE, 'DD-MON-RR', 'nls_date_language=english') dt FROM DUAL;
DT
---------
26-OCT-15
Also, the NLS_LANG value might not be correctly set in the OS environmental variable. See Why are junk values/special characters/question marks displayed on my client?

Culture-independent format for date

I'm calling Oracle procedures that have argument date. How can I specify the format of the date so it is recognized by oracle no matter what kind of globalization it uses ?
E.g.
create or replace procedure MyProc(p_valid_date in date) ...
The client calls the proc, how should I specify p_valid_date as a string ... ?
If I use, e.g., '01212014', i.e. DDMMYYYY then it won't work with US settings. If I use US settings, it won't work in UK. Etc.
In SQL Server, I'd use YYYYMMDD format which is culture invariant but I can't find anything about culture invariant format in Oracle documentation and all kind of formats that I have tried seem to be always bound to the culture.
UPDATE
I don't want to use functions because my client does pure procedure call. That's a limitation of my client. I know that I could change the argument into string and then convert the string using to_date inside the procedure but that seems like a pretty poor design.
Thank you
ISO date format is "invariant" you are looking for:
SQL> alter session set nls_date_format='dd/mon.yyyy';
Session altered.
SQL> select sysdate from dual;
SYSDATE
-----------
07/nov.2014
SQL> select to_char(DATE '2014-12-31') from dual;
TO_CHAR(DAT
-----------
31/dec.2014
You can use this notation: DATE '2014-12-31'

Oracle - How to Convert VARCHAR to DATETIME format

I am using Oracle10g database in which a table contains a Column with Date DataType. I am using the following query to get the record:
select to_char(START_TIME, 'YYMMDD HH24:MI:SS') from table;
So from above query, the result will be of type VARCHAR. I have tried to_Date() method but resulted in displaying only DATE. Can i convert VARCHAR to DATETIME format? The result should be of type DATETIME. Please help me how to resolve this problem.
an Oracle date contains both date and time so you can consider it a datetime (there is no datatype of datetime in Oracle). how is DISPLAYS when you select it is entirely up to your client. the default display setting is controlled by the NLS_DATE_FORMAT parameter. If you're just using the date in your pl/sql block then just assign it into a date datatype and select into that variable without to_char and it will work just fine and contain whatever time component is present in your table.
to control the display, for example using nls_date_format:
SQL> select a from datetest;
A
---------
19-FEB-13
SQL> alter session set nls_date_format='YYMMDD HH24:MI:SS';
Session altered.
SQL> select a from datetest;
A
---------------
130219 07:59:38
but again, this is only for display.
Oracle's Date type fields contain date/time values, therefore converting it to Datetime does not make any sense (it's already datetime)
Read more about oracle date types here
Yeah the Date datatype will meet your needs but you will have to jump through some hoops every time to get the exact time out of it. Definitely use the Timestamp datatype.

Resources