Usage of Oracle nvl functions

Usage of oracle's nvl function

Obtain the total value of a field by querying, if the value is null , it will give a preset default value 
 select nvl( sum (t.dwxhl),1) from tb_jhde t where zydm=-1
The usage of nvl concerned here, nvl(arg, value ) means that if the value of the preceding arg is null , the returned value is the following value  
, such as: NVL(a,b) is to judge whether a is NULL , if not return a value, if it is the value of returning b, the total value of a field is obtained by querying, if this value is null , a preset default value will be given

Another useful method about
declare 
i integer 
select nvl(sum(t.dwxhl),1) into i from tb_jhde t where zydm=-1
In this way, the obtained total value can be stored in the variable i, and if the value of the query is null , its value is set to the default 1

Nvl Functions under Oracle The nvl( ) function returns a non- null value from two expressions.
grammar
NVL(eExpression1, eExpression2)
parameter
eExpression1, eExpression2
If eExpression1 evaluates to a null value, NVL( ) returns eExpression2. Returns eExpression1 if eExpression1 does not evaluate to a null value. eExpression1 and eExpression2 can be of any data type. If the results of eExpression1 and eExpression2 are both null values, NVL( ) returns .NULL.
return value type
character, date, datetime, numeric, currency, logical, or null
illustrateNVL( ) can be used to remove null values ​​from calculations or operations in cases where null values ​​are 
not supported or are insignificant . select nvl(a.name, 'empty' ) as name from student a join school b on a.ID=b.ID Note: The types of the two parameters must match. Q: What is NULL ? Answer: When we don't know the specific data, that is, unknown, we can use NULL . We call it empty. In ORACLE, the length of the table column containing the empty value is zero.  
ORACLE allows fields of any data type to be null, except in the following two cases:

1. The primary key field ( primary  key ),
2. Fields 
that have been defined with NOT  NULL restrictions

illustrate:

1. Equivalent to no value and an unknown.

2. NULL is different from 0, empty string, and space.

3. Perform addition, subtraction, multiplication, division and other operations on the empty value, and the result is still empty.

4. The processing of NULL uses the NVL function.

5. Use keywords " is  null " and " is  not  null " when comparing.

6. Null values ​​cannot be indexed, so some qualified data may not be found during query. In count (*), use nvl (column name, 0) to process and then check.

7. When sorting, it is larger than other data (the default index is in descending order, small → large), so the NULL value is always ranked last.
Instructions:
SQL > select 1 from dual where  null = null ; no record found 
 SQL > select 1 from dual where  null = '' ; no record found 
 SQL > select 1 from dual where  '' = '' ; no record found 
 SQL > select 1 from dual where  null  is  null ; 1 --------- 1 
 SQL > select 1from dual where nvl( null ,0)=nvl( null ,0); 1 --------- 1 
 Add, subtract, multiply, divide and other operations on null values, and the result is still null. 
SQL > select 1+ null  from dual; 
 SQL > select 1- null  from dual; 
 SQL > select 1* null  from dual; 
 SQL > select 1/ null  from dual; Query a record. Note: This record is the one in the SQL statement that null set some columns to null update table1 setcolumn 1 = NULL  where column 1 is  not  null ;
There is a product sales table sale

The table structure is: month   char (6)   --month sellnumber(10,2) --monthly sales amount
 
create  table  sale ( month  char (6),sell number );
 insert  into sale values ​​( '200001' ,1000);
 insert  into sale values ​​( '200002' ,1100);
 insert  into sale values ​​( '200003' ,1200);
 insert  into sale values ​​( '200004' ,1300);
 insert  into salevalues('200005',1400);
insert into sale values('200006',1500);
insert into sale values('200007',1600);
insert into sale values('200101',1100);
insert into sale values('200202',1200);
insert into sale values('200301',1300);
insert into sale values('200008',1000);
insert  into sale( month ) values ​​( '200009' ); (Note: the sell value of this record is empty) 
 SQL > select * from sale where sell like  '%' ;

MONTH        SELL
------ ----------
200001       1000
200002       1100
200003       1200
200004       1300
200005       1400
200006       1500
200007       1600
200101       1100
200202       1200
200301       1300
200008       1000

11 rows selected.
Query to 11 records. Result description: Query result description This SQL statement cannot query the field whose column value is NULL . In this case, the case where the field is NULL needs to be handled separately.

SQL> select * from sale where sell like '%' or sell is null;
SQL> select * from sale where NVL(sell, 0) like '%';

MONTH        SELL
------ ----------
200001       1000
200002       1100
200003       1200
200004       1300
200005       1400
200006       1500
200007       1600
200101       1100
200202       1200
200301       1300
200008       1000

MONTH        SELL
------ ----------
200009

12 rows selected.

This is how Oracle's null value is used, and we'd better be familiar with its conventions in case the result is incorrect.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327015758&siteId=291194637