sql statement determined null value function

The COALESCE () function
mainstream database systems support the COALESCE () function, which is mainly used for processing null values, the parameters in the following format: 
the COALESCE (expression The, VALUE1, value2 ......, valueN) 
the first parameter the COALESCE () function expression is an expression to be detected, and thereafter indefinite number of parameters.
COALESCE () function will return all parameters, including expression, including the first non-null expression.

If the expression is not null expression is returned; otherwise determine whether value1 is null,

If value1 is not null value is returned value1; value2 otherwise determine whether the value is empty,

If value2 is not null is returned value2; ...... so,
if all the expressions are null values, NULL is returned. 


We will use the COALESCE () function to perform the following functions return to the "important dates" personnel:

If the date of birth is not empty then the date of birth as an "important date", date of birth is empty if it is determined whether the registered date is empty, if the date is not registered as an "important date" is empty then the date of registration, if the registration date will also be empty, "August 8, 2008" as "an important date." SQL statement to achieve this function as follows: 
MYSQL, the MSSQLServer, the DB2: 
 

  1. SELECT FName,FBirthDay,FRegDay, 
  2. COALESCE(FBirthDay,FRegDay, '2008-08-08')  AS ImportDay  
  3. FROM T_Person 


Oracle: 
 

  1. SELECT FBirthDay,FRegDay,  
  2. COALESCE(FBirthDay,FRegDay, TO_DATE('2008-08-08', 'YYYY-MM-DD HH24:MI:SS'))  
  3. AS ImportDay  
  4. FROM T_Person 


  It is finished we can see the results in the following output results: 

  1. FName   FBirthDay  FRegDay  ImportDay 
  2. Tom  1981 -03-22 00:00:00  1998-05-01 00:00:00  1981-03-22 00:00:00 
  3. Jim  1987 -01-18 00:00:00  1999-08-21 00:00:00  1987-01-18 00:00:00 
  4. Lily  1987 -11-08 00:00:00  2001-09-18 00:00:00  1987-11-08 00:00:00 
  5. Kelly  1982 -07-12 00:00:00  2000-03-01 00:00:00  1982-07-12 00:00:00 
  6. Sam  1983 -02-16 00:00:00  1998-05-01 00:00:00  1983-02-16 00:00:00 
  7. Kerry  < NULL>  1999-03-01 00:00:00  1999-03-01 00:00:00 
  8. Smith  < NULL>  <NULL>  2008-08-08 
  9. BillGates 1972 -07-18 00 : 00 : 00 1995 -06-19 00 : 00 : 00 1972 -07-18 00 : 00 : 00 


The key here is to side and Kerry Smith two lines, you can see the calculation logic here is fully in line with our
needs.

The COALESCE () function can be used to perform almost all of the null processing, but in many database systems provide a simplified version of it, these two simplified version only accepts variables, the parameters in the following format: 
MYSQL Based: 
  the IFNULL (expression The, value) 
the MSSQLServer: 
  ISNULL (expression The, value) 
the Oracle: 
  NVL (expression The, value) 

These functions and features COALESCE (expression, value) are equivalent.

For example, the SQL statement used to return the personnel of "important dates", if the date of birth is not empty then the date of birth as an "important date", date of birth is empty if the registration date of the return value: 
MYSQL: 
the SELECT FBirthDay, FRegDay,  
IFNULL (FBirthDay, FRegDay) AS ImportDay  
the FROM T_Person 
the MSSQLServer: 
the SELECT FBirthDay, FRegDay,  
ISNULL (FBirthDay, FRegDay) AS ImportDay  
the FROM T_Person 
the Oracle: 
the SELECT FBirthDay, FRegDay,  
NVL (FBirthDay, FRegDay) AS ImportDay  
the FROM T_Person 

Guess you like

Origin www.cnblogs.com/mark5/p/11527266.html