Q
Problem solve Get help with specific problems with your technologies, process and projects.

COALESCE and dates

How do you use COALESCE to replace a "null" return, if the datatype for the column is date? Example: I am calling...

back dates of last performance reviews for employees. When the employee has not had a performance review, the return is "null", and I want to substitute something else.

The trick here is to convert the date to character data (using the CAST function) before applying the COALESCE function. Here's an example in Oracle flavored SQL:

create table Invoices
( InvoiceNo numeric(5) primary key,
  InvoiceSent date
);

insert into Invoices values (10001, '03-DEC-2001');
insert into Invoices values (10002, NULL);
insert into Invoices values (10003, '16-FEB-2002');
insert into Invoices values (10004, '25-JAN-2002');
insert into Invoices values (10005, NULL);

select InvoiceNo "Invoice No", 
       coalesce(cast(InvoiceSent as char(11)),
                '--not sent--') "Invoice Sent"
  from Invoices;

Invoice No Invoice Sent
---------- ------------
     10001 03-DEC-2001
     10002 --not sent--
     10003 16-FEB-2002
     10004 25-JAN-2002
     10005 --not sent--

For More Information

  • What do you think about this answer? E-mail the edtiors at editor@searchDatabase.com with your feedback.
  • The Best SQL Web Links: tips, tutorials, scripts, and more.
  • Have an SQL tip to offer your fellow DBAs and developers? The best tips submitted will receive a cool prize. Submit your tip today!
  • Ask your technical SQL questions -- or help out your peers by answering them -- in our live discussion forums.
  • Ask the Experts yourself: Our SQL, database design, Oracle, SQL Server, DB2, metadata, object-oriented and data warehousing gurus are waiting to answer your toughest questions.

Dig Deeper on Oracle and SQL

Start the conversation

Send me notifications when other members comment.

Please create a username to comment.

-ADS BY GOOGLE

SearchDataManagement

SearchBusinessAnalytics

SearchSAP

SearchSQLServer

TheServerSide.com

SearchDataCenter

SearchContentManagement

SearchHRSoftware

Close