Thursday, September 28, 2017

Delete duplicate rows from Oracle tables


Delete duplicate rows from Oracle tables

Removing duplicate rows from Oracle tables with SQL can be very tricky, and there are several techniques for identifying and removing duplicate rows from tables:
  •  Delete multiple duplicate rows
  • Subquery to identify duplicate rows
  • Use RANK to find and remove duplicate table rows
  • Use self-join to remove duplicate rows
  • Use analytics to detect and remove duplicate rows
  • Delete duplicate table rows that contain NULL values

Use subquery to delete duplicate rows

Here we see an example of using SQL to delete duplicate table rows using an SQL subquery to identify duplicate rows, manually specifying the join columns:
DELETE FROM
   table_name A
WHERE
  a.rowid >
   ANY (
     SELECT
        B.rowid
     FROM
        table_name B
     WHERE
        A.col1 = B.col1
     AND
        A.col2 = B.col2
        );

Use RANK to delete duplicate rows

This is an example of the RANK function to identify and remove duplicate rows from Oracle tables, which deletes all duplicate rows while leaving the initial instance of the duplicate row:

delete from $table_name where rowid in
(
select "rowid" from
(select "rowid", rank_n from
(select rank() over (partition by $primary_key order by rowid) rank_n, rowid as "rowid"
from $table_name
where $primary_key in
(select $primary_key from $table_name
group by $all_columns
having count(*) > 1
)
)
)
where rank_n > 1
)
One of the most important features of Oracle is the ability to detect and remove duplicate rows from a table. While many Oracle DBA place primary key referential integrity constraints on a table, many shops do not use RI because they need the flexibility.


Use self-join to delete duplicate rows

The most effective way to detect duplicate rows is to join the table against itself as shown below.
select
   book_unique_id,
   page_seq_nbr,
   image_key
from
   page_image a
where
   rowid >
     (select min(rowid) from page_image b
      where
         b.key1 = a.key1
      and
         b.key2 = a.key2
      and
         b.key3 = a.key3
      );


Please note that you must specify all of the columns that make the row a duplicate in the SQL where clause. Once you have detected the duplicate rows, you may modify the SQL statement to remove the duplicates as shown below:
delete from
   table_name a
where
   a.rowid >
   any (select b.rowid
   from
      table_name b
   where
      a.col1 = b.col1
   and
      a.col2 = b.col2
   )
;

Use analytics to delete duplicate rows

You can also detect and delete duplicate rows using Oracle analytic functions:


delete from
   customer
where rowid in
 (select rowid from
   (select
     rowid,
     row_number()
    over
     (partition by custnbr order by custnbr) dup
    from customer)
  where dup > 1);


As we see, there are several ways to detect and delete duplicate rows from Oracle tables

Reader Comments:

Removing duplicate table rows where rows have NULL values

Rob Arden states:  The tip on this page helped with removing duplicate rows from Oracle tables. I thought this might be useful so I'm passing it on: I needed to add a null check because this fails to remove dupe rows where the fields match on a null value.  So instead of the given:
delete from
   table_name a
where
   a.rowid >
   any (select b.rowid
   from
      table_name b
   where
      a.col1 = b.col1
   and
      a.col2 = b.col2
   )
;


I needed to do the following to remove all of the duplicate table rows:


delete from
   table_name a
where
   a.rowid >
   any (select b.rowid
   from
      table_name b
   where
      (a.col1 = b.col1 or (a.col1 is null and b.col1 is null))
   and
      (a.col2 = b.col2 or (a.col2 is null and b.col2 is null))
   )
;

Sunday, September 24, 2017

Oracle rename table and Column from sql command


rename
-------
ALTER TABLE old_table RENAME TO new_table;
/
alter table table_name rename column old_column_name  TO new_column_name;

alter table it_item_serial rename column serialno TO serial;


//

alter table uer_module rename to user_module;


encrypt /decrypt for password procedure

 encrypt /decrypt


create or replace PACKAGE toolkit AS
  FUNCTION encrypt (p_text  IN  VARCHAR2) RETURN RAW;
  FUNCTION decrypt (p_raw  IN  RAW) RETURN VARCHAR2;
END toolkit;

//
create or replace PACKAGE BODY toolkit AS

  g_key     RAW(32767)  := UTL_RAW.cast_to_raw('12345678');
  g_pad_chr VARCHAR2(1) := '~';

  PROCEDURE padstring (p_text  IN OUT  VARCHAR2);


  -- --------------------------------------------------
  FUNCTION encrypt (p_text  IN  VARCHAR2) RETURN RAW IS
  -- --------------------------------------------------
    l_text       VARCHAR2(32767) := p_text;
    l_encrypted  RAW(32767);
  BEGIN
    padstring(l_text);
    DBMS_OBFUSCATION_TOOLKIT.desencrypt(input          => UTL_RAW.cast_to_raw(l_text),
                                        key            => g_key,
                                        encrypted_data => l_encrypted);
    RETURN l_encrypted;
  END;
  -- --------------------------------------------------



  -- --------------------------------------------------
  FUNCTION decrypt (p_raw  IN  RAW) RETURN VARCHAR2 IS
  -- --------------------------------------------------
    l_decrypted  VARCHAR2(32767);
  BEGIN
    DBMS_OBFUSCATION_TOOLKIT.desdecrypt(input => p_raw,
                                        key   => g_key,
                                        decrypted_data => l_decrypted);

    RETURN RTrim(UTL_RAW.cast_to_varchar2(l_decrypted), g_pad_chr);
  END;
  -- --------------------------------------------------


  -- --------------------------------------------------
  PROCEDURE padstring (p_text  IN OUT  VARCHAR2) IS
  -- --------------------------------------------------
    l_units  NUMBER;
  BEGIN
    IF LENGTH(p_text) MOD 8 > 0 THEN
      l_units := TRUNC(LENGTH(p_text)/8) + 1;
      p_text  := RPAD(p_text, l_units * 8, g_pad_chr);
    END IF;
  END;
  -- --------------------------------------------------
////

create or replace TRIGGER ITINV.encrypted_data_biur_trg
      BEFORE INSERT OR UPDATE ON USER_INFO  FOR EACH ROW
DECLARE
 BEGIN
      :new.USER_PASS := toolkit.encrypt(:new.USER_PASS);
 END;

Thursday, September 21, 2017

ALL Relational Operator in Oracle


composite fk 
ALTER TABLE table_name
ADD CONSTRAINT constraint_name
FOREIGN KEY (column1, column2, ... column_n)
REFERENCES parent_table (column1, column2, ... column_n);



it inventory software


create table it_item
(
itemcode               varchar2(30),  ---pk 
itemname               varchar2(120),
uom_code               varchar2(3),   ---fk
cat_code               varchar2(10),  ---fk
brand_code             varchar2(10),  ---fk
item_status            varchar2(1),
user_id                varchar2(6),
wk_dt                  varchar2(20)   default to_char(sysdate,'DD-MON-RR HH24:MI:SS')
)
/
alter table it_item add constraint it_item_pk  primary key(itemcode,itemname)
/
alter table it_item add constraint it_item_fk foreign key(uom_code) references it_item_uom(uom_code)
/
alter table it_item add constraint it_item_category_fk foreign key(cat_code) references it_item_category(cat_code)
/
alter table it_item add constraint it_item_brand_fk foreign key(brand_code) references it_item_brand(brand_code)
/


create table it_item_uom
(
uom_code               varchar2(3),  ---pk  
uom_nm                 varchar2(3)
)
/
alter table it_item_uom add constraint it_item_uom_pk  primary key(uom_code)
/



create table it_item_category
(
cat_code              varchar2(10),   ---pk 
cat_nm                varchar2(50)
)
/
 alter table it_item_category add constraint it_item_category_pk  primary key(cat_code)
/


create table it_item_brand 
(
brand_code              varchar2(10),   ---pk
brand_nm                varchar2(50)   
)
/
alter table it_item_brand add constraint it_item_brand_pk  primary key(brand_code)
/


create table it_receipr_master
(
recpt_no              varchar2(30),   ---pk
recpt_dt              date,
slcode                varchar2(6),   ----
slname                varchar2(70),
prs_no                varchar2(30),
prs_dt                varchar2(30),
remk                  varchar2(200),
recpt_by              varchar2(6),
user_id               varchar2(6),
wk_dt                 varchar2(20) default  to_char(sysdate,'DD-MON-RR HH24:MI:SS')
)
/
alter table it_receipr_master add constraint it_receipr_master_pk  primary key(recpt_no)
/


create table it_receipt_detail
(
recpt_no             varchar2(30),  ---com.pk 
slno                 number(30),    ---com.pk
itemcode             varchar2(30),  ---fk   
qty                  number(20,2),
unit_price           number(20,2),
tot_price            number(20,2)
)
alter table it_receipt_detail add constraint it_receipt_detail_pk primary key(recpt_no,sl_no,itemcode)
/
alter table it_receipt_detail add constraint it_item_fk foreign key(itemcode) references it_item(itemcode)
/
alter table it_receipt_detail add constraint it_receipr_master_fk foreign key(recpt_no) references it_receipr_master(recpt_no)
/




create table it_emp_delivery_master
(
del_no               varchar2(30),   --pk  
del_dt               date,
emp_id               varchar2(6),    ---- fk view
remk                 varchar2(150),
del_by               varchar2(6),
user_id              varchar2(6),
wk_dt                varchar2(20)     default  to_char(sysdate,'DD-MON-RR HH24:MI:SS')
)
/

alter table it_emp_delivery_master add constraint it_emp_delivery_master_pk  primary key(del_no)
/


create table it_emp_delivery_detail
(
del_no               varchar2(30),   ---com.pk
slno                 number(30),     ---com.pk
itemcode             varchar2(30),   ---fk 
qty                  number(10,2),
item_desc            varchar2(150)
)

alter table it_emp_delivery_detail add constraint br_receipt_detail_pk primary key(del_no,slno,itemcode)
/
alter table it_emp_delivery_detail add constraint it_emp_delivery_detail_fk foreign key(itemcode) references it_item(itemcode)
/
alter table it_emp_delivery_detail add constraint it_emp_delivery_master_fk foreign key(del_no) references it_emp_delivery_master(del_no)
/



create table it_return_master
(
rtn_no               varchar2(30),     ---pk
rtn_dt               date,
emp_id               varchar2(30),     ---fk view 
rtn_desc             varchar2(200),
del_by               varchar2(6),
user_id              varchar2(6),
wk_dt                varchar2(20)     default  to_char(sysdate,'DD-MON-RR HH24:MI:SS')
)
/
alter table it_return_master add constraint it_return_master_pk  primary key(rtn_no)





create table it_return_detail
(
rtn_no               varchar2(30),   ---com.pk
slno                 number(30),     ---com.pk
itemcode             varchar2(30),   ---fk   
qty                  number(10,2),
item_desc            varchar2(150)
)  

alter table it_return_detail add constraint it_return_detail_pk primary key(recpt_no,slno,itemcode)
/
alter table it_return_detail add constraint it_return_detail_fk foreign key(itemcode) references it_item(itemcode)
/
alter table it_return_detail add constraint it_return_master_fk foreign key(rtn_no) references it_return_master(rtn_no)
/


create table it_wastage_master
(
wast_no             varchar2(30),   ---pk
wast_dt             date,
emp_id              varchar2(6),    ---fk view
remk                varchar2(150),
wast_by             varchar2(6),
user_id             varchar2(6),
wk_dt               varchar2(20)     default  to_char(sysdate,'DD-MON-RR HH24:MI:SS')

)
/
alter table it_wastage_master add constraint it_wastage_master_pk  primary key(wast_no)

/



create table it_wastage_detail
(
wast_no             varchar2(30),  ---com.pk  ,fk
slno                varchar2(30),  ---com.pk
itecode             varchar2(30),
qty                 number(10,2),
item_desc           varchar2(150)
)
/
alter table it_wastage_detail add constraint it_wastage_detail_pk primary key(wast_no,slno,itemcode)
/
alter table it_wastage_detail add constraint it_wastage_detail_fk foreign key(itemcode) references it_item(itemcode)
/
alter table it_wastage_detail add constraint it_wastage_master_fk foreign key(wast_no) references it_wastage_master(wast_no)
/





create table it_emp_issue_info
(
tag_sl               varchar2(50),    ---pk 
del_no               varchar2(30),    ---fk 
emp_id               varchar2(6),     ---fk
computer_code        varchar2(30),    ---fk
cpu_serialno         varchar2(50),
cpu_brand            varchar2(50),
cpu_qty              number(4,2),
cpu_desc             varchar2(100),
ups_serial           varchar2(50),
ups_brand            varchar2(50),
ups_qty              number(4,2),
ups_desc             varchar2(100),
monitor_serialno     varchar2(50),
monitor_brand        varchar2(50),
monitor_qty          varchar2(4,2),
keyboard_brand       varchar2(50),
keyboard_qty         number(4,2),
mouse_brand          varchar2(50),
mouse_qty            number(4,2),
laptop_bag_brand     varchar2(50),
laptop_bag_qty       number(4,2),
mouse_pad_brand      varchar2(50),
mouse_pad_qty        number(4,2),
printer_serialno     varchar2(50),
printer_brand        varchar2(50),
scanner_serialno     varchar2(50),
scanner_brand        varchar2(50),
photocopy_serialno   varchar2(50),
photocopy_brand      varchar2(50),
pen_drive_serialno   varchar2(50),
pen_drive_brand      varchar2(50),
modem_brand          varchar2(50),
modem_qty            number(4,2),
email                varchar2(100),
fax_no               varchar2(50),
telephone            varchar2(50),
ext_no               varchar2(50),
ip_address           varchar2(50),
anti_virus           varchar2(50),
warranty_period      number(4,2),
anti_virus_desc      varchar2(100),
software_desc        varchar2(100),
net_access           varchar2(1),
wifi_access          varchar2(1),
software_access      varchar2(1),
status               varchar2(1),
flag                 varchar2(1),
remk                 varchar2(150),
user_id              varchar2(6),
wk_dt                varchar2(20)    default  to_char(sysdate,'DD-MON-RR HH24:MI:SS') 


)


/


create table computer_type
(
computer_code       varchar2(30),  ---pk
computer_nm         varchar2(50),
computer_typ        varchar2(50)
)
/
alter table computer_type add constraint computer_type_pk primary key(computer_code)
/




create view emp_mast_vw
(
com_cd                 varchar2(2),
emp_id                 varchar2(6),
emp_nm                 varchar2(50),
desg_cd                varchar2(3),
loc_cd                 varchar2(2),
cost_sl                varchar2(2),
dept_cd                varchar2(2),
sec_cd                 varchar2(2),
join_dt                date,
resig_dt               date,
sal                    varchar2(1),
flag                   varchar2(1),
conf_fg                varchar2(1),
wk_dt                  date,
user_id                varchar2(6),
div_cd                 varchar2(2),
emp_type               varchar2(1),
mgr_id                 varchar2(6)
)
















Tuesday, September 19, 2017

User log on and log off history in oracle database on oracle forms 10




http://www.orafaq.com/wiki/Forms_FAQ

create table logon_off_hist
(
user_id          varchar2(6),
machine_ip       varchar2(50),
machine_nm       varchar2(64),
module_nm        varchar2(30),
dt               date  default  to_char(sysdate,'DD-MON-RR'),
logon_off_time   varchar2(20) default  to_char(sysdate,'DD-MON-RR HH24:MI:SS'),
st               varchar2(1)
)


///

--logon button

DECLARE

machine_ip varchar2(50);
machine_nm varchar2(64);


BEGIN

 SELECT SYS_CONTEXT('USERENV','IP_ADDRESS') into machine_ip FROM dual;
 select distinct TERMINAL into machine_nm from v$session where USERNAME='ITS'; -- MACHINE
 INSERT INTO logon_off_hist(user_id,machine_ip,machine_nm, module_nm,st)
 VALUES (:user_id,machine_ip,machine_nm,'ITS','I');

 COMMIT_FORM;
 clear_message;

 END;


 /

--key_exit trigger/exit button  login form two path

DECLARE

machine_ip varchar2(50);
machine_nm varchar2(64);


BEGIN

 SELECT SYS_CONTEXT('USERENV','IP_ADDRESS') into machine_ip FROM dual;
 select distinct TERMINAL into machine_nm from v$session where USERNAME='ITS'; -- MACHINE
 INSERT INTO logon_off_hist(user_id,machine_ip,machine_nm, module_nm,st)
 VALUES (:user_id,machine_ip,machine_nm,'ITS','O');

 COMMIT_FORM;
 clear_message;

 END;




Monday, September 18, 2017

SQL Command Line SET Commands

To view all the settings, enter the following at the SQL prompt



SQL*Plus: Release 11.2.0.1.0 Production on Mon Sep 18 21:24:27 2017

Copyright (c) 1982, 2010, Oracle.  All rights reserved.

Enter user-name: fhrd/fhrd@orcl

Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

SQL> SHOW ALL
appinfo is OFF and set to "SQL*Plus"
arraysize 15
autocommit OFF
autoprint OFF
autorecovery OFF
autotrace OFF
blockterminator "." (hex 2e)
btitle OFF and is the first few characters of the next SELECT statement
cmdsep OFF
colsep " "
compatibility version NATIVE
concat "." (hex 2e)
copycommit 0
COPYTYPECHECK is ON
define "&" (hex 26)
describe DEPTH 1 LINENUM OFF INDENT ON
echo OFF
editfile "afiedt.buf"
embedded OFF
escape OFF
escchar OFF
exitcommit ON
FEEDBACK ON for 6 or more rows
flagger OFF
flush ON
heading ON
headsep "|" (hex 7c)
instance "local"
linesize 80
lno 14
loboffset 1
logsource ""
long 80
longchunksize 80
markup HTML OFF HEAD "SQL*Plus Report" BODY "" TABLE "border='1' width='90%' align='center' summary='Script output'" SPOOL OFF ENTMAP ON PREFORMAT OFF
newpage 1
null ""
numformat ""
numwidth 10
pagesize 14
PAUSE is OFF
pno 0
recsep WRAP
recsepchar " " (hex 20)
release 1102000100
repfooter OFF and is NULL
repheader OFF and is NULL
securedcol is OFF
serveroutput OFF
shiftinout INVISIBLE
showmode OFF
spool OFF
sqlblanklines OFF
sqlcase MIXED
sqlcode 0
sqlcontinue "> "
sqlnumber ON
sqlpluscompatibility 11.2.0
sqlprefix "#" (hex 23)
sqlprompt "SQL> "
sqlterminator ";" (hex 3b)
suffix "sql"
tab ON
termout ON
timing OFF
trimout ON
trimspool OFF
ttitle OFF and is the first few characters of the next SELECT statement
underline "-" (hex 2d)
USER is "FHRD"
verify ON
wrap : lines will be wrapped
errorlogging is OFF



SQL> SET PAGESIZE 200
SQL> SET LINESIZE 140


To enable output from PL/SQL blocks with DBMS_OUTPUT.PUT_LINE, use the following:
SQL> SET SERVEROUTPUT ON


Running Scripts From SQL Command Line

You can use a text editor to create SQL Command Line script files that contain SQL*Plus, SQL, and PL/SQL statements. For consistency, use the .sql extension for the script file name.
A SQL script file is executed with a START or @ command. For example, in a Windows environment, you can execute a SQL script as follows:
SQL> @c:\my_scripts\my_sql_script.sql
A SQL script file can be executed in a Linux environment as follows:
SQL> START /home/cjones/my_scripts/my_sql_script.sql
You can use SET ECHO ON to cause a script to echo each statement that is executed. You can use SET TERMOUT OFF to prevent the script output from displaying on the screen.
When running a script, you need to include the full path name unless the script is located in the directory from which SQL Command Line was started, or the script is located in the default script location specified by the SQLPATHenvironment variable.




SET MARKUP HTML PREFORMAT ON
SET COLSEP '|'
SELECT LAST_NAME, JOB_ID, DEPARTMENT_ID
FROM EMP_DETAILS_VIEW
WHERE DEPARTMENT_ID = 20;
LAST_NAME                |JOB_ID    |DEPARTMENT_ID
-------------------------|----------|-------------
Hartstein |MK_MAN | 20
Fay |MK_REP | 20


How to check the Oracle database version


SELECT * FROM V$VERSION
or
SELECT version FROM V$INSTANCE
or
SET SERVEROUTPUT ON
BEGIN DBMS_OUTPUT.PUT_LINE(DBMS_DB_VERSION.VERSION || '.' || DBMS_DB_VERSION.RELEASE); END;

Figure 1-1 Example of an Oracle Database Release Number

Description of Figure 1-1 follows


Major Database Release Number



The first digit is the most general identifier. It represents a major new version of the software that contains significant new functionality.






Database Maintenance Release Number



The second digit represents a maintenance release level. Some new features may also be included.






Application Server Release Number



The third digit reflects the release level of the Oracle Application Server (OracleAS).






Component-Specific Release Number



The fourth digit identifies a release level specific to a component. Different components can have different numbers in this position depending upon, for example, component patch sets or interim releases.






Platform-Specific Release Number



The fifth digit identifies a platform-specific release. Usually this is a patch set. When different platforms require the equivalent patch set, this digit will be the same across the affected platforms.


SQL> COL PRODUCT FORMAT A35
SQL> COL VERSION FORMAT A15
SQL> COL STATUS FORMAT A15
SQL> SELECT * FROM PRODUCT_COMPONENT_VERSION;

PRODUCT VERSION STATUS
----------------------------------- --------------- ---------------
NLSRTL 11.2.0.1.0 Production
Oracle Database 11g Enterprise Edit 11.2.0.1.0 Production
ion

PL/SQL 11.2.0.1.0 Production
TNS for 32-bit Windows: 11.2.0.1.0 Production


How to find the latest SQL statements within the database?



Step 1: Determine the installatin IDs & user IDs.

SELECT inst_id,sid FROM gv$session WHERE username='';
Step 2:
SELECT 
s
.sid
,s.CLIENT_INFO
,s.MACHINE
,s.PROGRAM
,s.TYPE
,s.logon_time
,s.osuser
,sq.sorts
,sq.DISK_READS
,sq.BUFFER_GETS
,sq.ROWS_PROCESSED
,sq.SQLTYPE
,sq.SQL_TEXT
FROM gv$session s
, gv$sql sq
WHERE s.SQL_HASH_VALUE = sq.HASH_VALUE
AND s.inst_id=:id -- replace with ID from above
AND s.sid = :sid -- replace with instID from above
AND sq.inst_id= s.inst_id
 select * from global_name;

Sunday, September 10, 2017

when table have no data ORA-01476 divisor is equal to zero tips in table sql

   SQL>  select nvl(ROUND(((NVL(SUM(45),0)/NULLIF(SUM(0),0))*100),2),0) divissor_error_remove from dual;

  DQTY_PER
----------
         0
//

or,

function RQTY_IQTY_PERFormula return Number is
begin
        if :RQTY<=0 then
        return(0);
    else
    return ((:IQTY/:RQTY)*100);
    end if;
end;



1. IS [NOT] NULL

Syntax: expr1 IS [NOT] NULL
SELECT * FROM emp t WHERE t.comm IS NULL;

2. NVL
Syntax: NVL(expr1, expr2)

if expr1 contains a NULL value, then replace it with the value of expr2
--The NVL function lets you substitute a value when a null value is encountered.

Examples:
NVL(‘A’,’B’) results in A
NVL(NULL,’B’) results in B
NVL(1,2) results in 1
NVL(NULL,2) results in 2
NVL(‘A’,2) results in A
NVL(1, ‘B’) results in an error


3.NVL2

Syntax: NVL2(expr1, expr2, expr3)

If expr1 contains a NULL value, then return expr3. If the value of expr1 contains a non-NULL value, then return expr2.

Examples:
NVL2(‘A’,’B’,’C’) results in B
NVL2(NULL,’B’,’C’) results in C
NVL2(1,2,3) results in 2
NVL2(NULL,2,3) results in 3


4. NULLIF

Syntax: NULLIF(expr1, expr2)


NULLIF returns NULL if expr1 is equal to expr2. If they are not equal expr1 is returned.
--Expressions must be of the same data type, There is no implicit conversion performed.

Examples:
NULLIF(‘A’,’B’) results in A
NULLIF(‘A’,’A’) results in NULL
NULLIF(2,3) results in 2
NULLIF(2,2) results in NULL
NULLIF(‘2’,2) results in an error
NULLIF(2,’2’) results in an error


5. COALESCE

Syntax: COALESCE(expr [, expr ]...)

The coalesce function returns the first non-NULL value of the expressions in the list. The list
must consist of at least 2 values. If all expressions evaluate to NULL then NULL is returned.

Examples:
COALESCE(‘A’,’B’,’C’) results in A
COALESCE(NULL,’B’,’C’) results in B
COALESCE(NULL,NULL,’C’) results in C
COALESCE(NULL,’B’,NULL) results in B
COALESCE(‘A’) results in an error


6. LNNVL

Syntax: LNNVL(condition)


The LNNVL function is used in the WHERE clause of an SQL statement when one of the
operands may contain a NULL value. The function returns TRUE is the result of the
condition is FALSE and FALSE is the result of the condition is TRUE or UNKNOWN. LNNVL
can be used in a condition when you would otherwise need to combine a condition with an
IS [NOT] NULL or an NVL predicate.

The following queries have the same result:

SELECT *
      FROM emp e
     WHERE LNNVL(e.comm >= 100);
    SELECT *
      FROM emp e
     WHERE 1 = 1
       AND ((e.comm))
      
      
7. DECODE

Syntax: DECODE(expr, search, result [, search, result ]... [, default ])

SELECT decode(color, 1, 'Red', 2, 'White', 3, 'Blue', 4, 'Yellow')
FROM table;

8. CASE

Syntax:

         CASE [ expression ]

   WHEN condition_1 THEN result_1
   WHEN condition_2 THEN result_2
   ...
   WHEN condition_n THEN result_n

   ELSE result

END;

The case statement has two flavors. The simple case and the searched case. In the simple case the expression is
written only once and depending on the result of this expression one of the possible cases is being used. This can be
helpful when you need to choose from a set of distinct values. In the searched case every case has its own expression.
This can for instance be helpful when using ranges.
The same query as above can be written using a simple case expression.

SELECT CASE color
         WHEN 1 THEN 'Red'
         WHEN 2 THEN 'White'
         WHEN 3 THEN 'Blue'
         WHEN 4 THEN 'Yellow'
         ELSE 'Unknown'
      END color
  FROM table;

The query can also be written using a searched case expression:

SELECT CASE
         WHEN color=1 THEN 'Red'
         WHEN color=2 THEN 'White'
         WHEN color=3 THEN 'Blue'
         WHEN color=4 THEN 'Yellow'
      END color
FROM table;

To generate a PDF using JavaScript in Oracle APEX from a collection

  To generate a PDF using JavaScript in Oracle APEX from a collection, you can follow these steps: 1. Create a button or link on your APEX p...