Oracle COALESCE Function


/ Published in: SQL
Save to your folder(s)

A few examples of the Oracle COALESCE function.


Copy this code and paste it in your HTML
  1. DROP TABLE customers;
  2.  
  3. CREATE TABLE customers (
  4. first_name varchar2(100),
  5. last_name varchar2(100),
  6. country varchar2(20),
  7. full_address CLOB,
  8. employees NUMBER,
  9. start_date DATE
  10. );
  11.  
  12. INSERT INTO customers (first_name, last_name, country, full_address, employees, start_date) VALUES ('John', 'Smith', 'USA', '10 Long Road', 4, '12-APR-2010');
  13. INSERT INTO customers (first_name, last_name, country, full_address, employees, start_date) VALUES ('Sally', 'Jones', 'USA', '50 Market Street', 10, '04-JUL-2011');
  14. INSERT INTO customers (first_name, last_name, country, full_address, employees, start_date) VALUES ('Steve', 'Brown', 'Canada', '201 Flinders Lane', 15, '21-MAR-2009');
  15. INSERT INTO customers (first_name, last_name, country, full_address, employees, start_date) VALUES ('Mark', 'Allan', 'UK', '8 Smith Street', 23, '1-FEB-2001');
  16. INSERT INTO customers (first_name, last_name, country, full_address, employees, start_date) VALUES ('Adam', 'Cooper', 'USA', '14 Wellington Road', 55, NULL);
  17. INSERT INTO customers (first_name, last_name, country, full_address, employees, start_date) VALUES ('Josh', 'Thompson', NULL, '80 Victoria Street', 1, '10-FEB-2012');
  18. INSERT INTO customers (first_name, last_name, country, full_address, employees, start_date) VALUES ('Peter', 'Manson', 'France', '5 Johnson St', NULL, '16-OCT-2012');
  19. INSERT INTO customers (first_name, last_name, country, full_address, employees, start_date) VALUES (NULL, '', NULL, '155 Long Road', NULL, '16-OCT-2012');
  20.  
  21. SELECT * FROM customers;
  22.  
  23.  
  24. --Example 1
  25.  
  26. SELECT first_name, last_name, COALESCE(first_name, last_name) AS coal
  27. FROM customers;
  28.  
  29. --Example 2
  30.  
  31. SELECT country, last_name, COALESCE(country, last_name) AS coal
  32. FROM customers;
  33.  
  34.  
  35. --Example 3
  36. SELECT first_name, last_name, country, COALESCE(first_name, last_name, country) AS coal
  37. FROM customers
  38. WHERE start_date = '16-OCT-2012';
  39.  
  40. --Example 4
  41. SELECT first_name, full_address, employees, start_date, COALESCE(first_name, full_address, employees, start_date) AS coal
  42. FROM customers;
  43.  
  44. --Example 5
  45. SELECT first_name, last_name, COALESCE(NULL, NULL, first_name, NULL, NULL, NULL, last_name) AS coal
  46. FROM customers;
  47.  
  48. --Example 6
  49. SELECT first_name, COALESCE('', first_name) AS coal
  50. FROM customers;
  51.  
  52. --Example 7
  53. SELECT start_date, COALESCE(start_date, SYSDATE) AS coal
  54. FROM customers;
  55.  
  56. --Example 8
  57. SELECT country, LENGTH(country), employees
  58. FROM customers
  59. WHERE COALESCE(employees, LENGTH(country)) > 4

URL: http://www.databasestar.com/oracle-coalesce/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.