Change Currency Sign in Oracle Apex Number Format - oracle

When i'm using FML999G999G999G999G990D00 as my Number Format Mask in Oracle Apex, it shows Value as $800.00. I need to Replace $ with another Currency Sign.
How Could i do this ?

You need to modify the session parameter NLS_CURRENCY to change the currency:
SQL> ALTER SESSION SET NLS_CURRENCY='EUR';
Session altered.
SQL> SELECT to_char(800, 'FML999G999G999G999G990D00') FROM dual;
TO_CHAR(800,'FML999G999G999G999G9
---------------------------------
EUR800,00
Or with DBMS_SESSION:
SQL> BEGIN dbms_session.set_nls('NLS_CURRENCY', 'GBP'); END;
2 /
PL/SQL procedure successfully completed.
SQL> SELECT to_char(800, 'FML999G999G999G999G990D00') FROM dual;
TO_CHAR(800,'FML999G999G999G999G9
---------------------------------
GBP800,00
You can also specify the currency directly with TO_CHAR:
SQL> SELECT to_char(800, 'FML999G990D00', 'NLS_CURRENCY=''£''') FROM dual;
TO_CHAR(800,'FML999G9
---------------------
£800,00

Related

How to change date format '17.DEC.80' to '17.12.80' in Run SQL Command Line SCOTT Database?

How to change date format '17.DEC.80' to '17.12.80' in Run SQL Command Line SCOTT Database?
It depends on what 17.DEC.80 actually is; if it is a string, then to_date it first (using appropriate format model), and then apply to_char with the final format:
SQL> select to_char(to_date('17.DEC.80', 'dd.mon-yy'), 'dd.mm.yy') like_this from dual;
LIKE_THI
--------
17.12.80
SQL>
If it is a date datatype value, then just to_char it, e.g.
SQL> create table test (col date);
Table created.
SQL> insert into test values (date '1980-12-17');
1 row created.
SQL> select col from test;
COL
---------
17-DEC-80
SQL> select to_char(col, 'dd.mm.yy') like_this from test;
LIKE_THI
--------
17.12.80
SQL>
Or, alter session to set format model:
SQL> alter session set nls_date_format = 'dd.mm.yy';
Session altered.
SQL> select * from test;
COL
--------
17.12.80
SQL>
Though: we've had Y2K issue 20 years ago and learnt that 2-digits years should be avoided. I suggest you do that as well and use 4 digits for years.

Change table's column name using stored procedure in Oracle PL/SQL

I'm new to Oracle PL/SQL. I got a table name EMP_1812057(SAL, HIRDATE). How to change its column's names by creating a stored procedure? Can i do something like passing new name as a parameter of the procedure and then it will change the column name to that new name? Can you give me some examples, please? Thanks anyway.
I'm not sure what would be that procedure's real purpose (we don't modify column names that frequently, and we rarely do it in a procedure), but - you'd use dynamic SQL with appropriate ALTER TABLE statement.
For example, rename SAL column to SALARY:
SQL> desc emp_1812057
Name Null? Type
----------------------------------------- -------- ----------------------------
SAL NUMBER
HIREDATE DATE
SQL> create or replace procedure p_emp is
2 begin
3 execute immediate 'alter table emp_1812057 rename column sal to salary';
4 end;
5 /
Procedure created.
SQL> exec p_emp
PL/SQL procedure successfully completed.
SQL> desc emp_1812057
Name Null? Type
----------------------------------------- -------- ----------------------------
SALARY NUMBER
HIREDATE DATE
SQL>

set DATE column default value with another column(VARCHAR2(8)),error ora-00904

create table T_XXX
(
DATE_POST VARCHAR2(8) NOT NULL,
DATE_GET DATE DEFAULT TO_DATE(SUBSTR("DATE_POST",1,8),'YYYYMMDD')
);
Error:
ORA-00904:"DATE_POST"
Why does this happen?
My Oracle version is 12c
Nope, that won't work.
But, as you're on 12c, create a virtual column. For example:
SQL> alter session set nls_Date_format = 'dd.mm.yyyy';
Session altered.
SQL> create table test
2 (date_post varchar2(8),
3 date_get date generated always as (to_date(date_post, 'yyyymmdd'))
4 );
Table created.
SQL> insert into test (date_post) values ('20200501');
1 row created.
SQL> select * From test;
DATE_POS DATE_GET
-------- ----------
20200501 01.05.2020
SQL>

Oracle "Number" datatype precision

We are trying to store the values in a table which has a column of "Number" Datatype. The problem arises when we store a very small value , like "0.000001.
SQL> desc testing
Name Type Nullable Default Comments
---- ------ -------- ------- --------
A NUMBER Y
SQL> insert into testing values (0.00000001);
1 row inserted
SQL> select * from testing;
A
----------
0.001
1E-5
1E-8
0.0001
Is there a way, we can store and retrieve the absolute values, as in store, 0.00001 instead of 1E-5.
It is simply a DISPLAY issue. Set numformat properly.
For example,
SQL> create table t(a number);
Table created.
SQL> insert into t values(0.000000000000001);
1 row created.
SQL> select * from t;
A
----------
1.0000E-15
SQL> set numformat 9D9999999999999999
SQL> select * from t;
A
-------------------
.0000000000000010
SQL>
Update OP says the above didn't fix the issue when trying to send the number value to the application frontend. There must be something wrong with the locale-specific NLS settings that is being used by the application.
If the issue is with just the display, then you could convert it to char using to_char and proper format model.
SQL> create table t(a number);
Table created.
SQL> insert into t values(.000000000000000000001);
1 row created.
SQL> select * from t;
A
----------
1.0000E-21
SQL> select ltrim(to_char(a, '9999999D999999999999999999999', 'NLS_NUMERIC_CHARACTERS = ''.,''')) num from t
NUM
------------------------------
.000000000000000000001
SQL>
Note the use of ltrim.

Implicit conversion of date field in a pl/sql trigger?

I need to develop a trigger in PL/SQL (Oracle) before INSERT.
In this table there is a column (cdat) of type DATE.
Let's say i do INSERT INTO myTbl (123,'12/05/2011');
In my trigger the :NEW.CDAT is converted in the final date system or it's still a varchar?
Do I need to do a TO_DATE(:NEW.CDAT) to get the date value?
:NEW.CDAT will be a date. The :new and :old variables in triggers are always the type of the destination field.
I wasn't able to find anything in the Oracle documentation that confirms my statement, but I was able to devise some experimental proof:
CREATE TABLE test2 (a DATE);
CREATE OR REPLACE TRIGGER bu_test2
BEFORE INSERT OR UPDATE
ON test2
FOR EACH ROW
DECLARE
PROCEDURE type_test(in_type DATE) IS
BEGIN
DBMS_OUTPUT.put_line('date');
END;
PROCEDURE type_test(in_type VARCHAR2) IS
BEGIN
DBMS_OUTPUT.put_line('varchar2');
END;
BEGIN
type_test(:new.a);
END;
INSERT INTO test2
VALUES ('24-Mar-2011');
Since type_test is overloaded, Oracle will choose which procedure to use based on the type being passed in. The results of this script are:
Table created.
Trigger created.
date
1 row created.
You need to do a conversion if your session parameter 'NLS_DATE_FORMAT' is not 'mm/dd/yyyy'.
For example:
create table myTbl (id number, cdat date);
select *
from nls_session_parameters ns
where ns.parameter = 'NLS_DATE_FORMAT';
PARAMETER VALUE
-------------------------------------------------------
NLS_DATE_FORMAT DD-MON-RR
In this case, without a to_Date you'll get an error:
insert into myTbl
values
(123, '12/05/2011');
ORA-01843: not a valid month
You can change this paramter at session level, system level, etc.
zep#dev> alter session set NLS_DATE_FORMAT = 'mm/dd/yyyy';
Session altered
select *
from nls_session_parameters ns
where ns.parameter = 'NLS_DATE_FORMAT';
PARAMETER VALUE
-------------------------------------------------------------------------------------
NLS_DATE_FORMAT mm/dd/yyyy
insert into myTbl
(id, cdat)
values
(123, '12/05/2011');
1 row inserted
zep#dev> select *
2 from myTbl;
ID CDAT
---------- -----------
123 05/12/2011
Test on a row level trigger
truncate table Mytbl;
alter session set NLS_DATE_FORMAT = 'DD-MON-RR';
create or replace trigger befins_myTbl
before insert on myTbl
for each row
declare
begin
-- demo
:new.cdat := :new.cdat + numtoyminterval(1,'YEAR');-- (demo trigger add 1 year )
end;
insert into myTbl
(id, cdat)
values
(123, '12/05/2011');
Output: ORA-01843: not a valid month
alter session set NLS_DATE_FORMAT = 'mm/dd/yyyy';
insert into myTbl
(id, cdat)
values
(123, '12/05/2011');
commit;
select *
from myTbl;
Output
ID CDAT
---------- -----------
123 12/05/2012

Resources