Home > Ask the Oracle Experts > Questions & Answers > Dates and DECODE
Ask The Oracle Expert: Questions & Answers
EMAIL THIS

Dates and DECODE

Frank Kulash EXPERT RESPONSE FROM: Frank Kulash

Pose a Question
Other Oracle Categories
Meet all Oracle Experts
Become an Expert for this site
>
QUESTION POSED ON: 02 October 2003
How do I use the DECODE function with a date field? Please give me some examples. I am looking for exact date matching and date comparisons. I am using Oracle8i. If DECODE can not handle it, how do I go about it?

>
EXPERT RESPONSE
DECODE only does exact matching. For example, exact values representing "Today" and "Tomorrow" are easy to get, so you might use DECODE like this:
SELECT  due_dt,
        DECODE ( TRUNC (due_dt),
                 TRUNC (SYSDATE),     'Today',
                 TRUNC (SYSDATE + 1), 'Tomorrow',
                                      'Neither'
               )  AS due_text
FROM    tablex;

When you want to compare values (e.g., "Is date a before date b?," or "Is date a between dates b1 and b2?") Then the CASE statement is much more convenient.

SELECT  due_dt,
        CASE
            WHEN  TRUNC (due_dt) < TRUNC (SYSDATE)
                THEN  'Past'
            WHEN  TRUNC (due_dt) BETWEEN TRUNC (SYSDATE)
                                     AND TRUNC (SYSDATE) + 6
                       -- Note: BETWEEN includes both end-points, so
                       --   SYSDATE+0 through SYSDATE+6 is a full week
                THEN  'This Week'
                ELSE  'Later'
        END  AS due_text
FROM    tablex;

CASE is a feature of ANSI SQL introduced into Oracle in version 8.1. See the earlier question on DECODE versus CASE for more examples.


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary



Search and Browse the Expert Answer Center
Search and browse more than 25,000 question and answer pairs from more than 250 TechTarget industry experts.
Browse our Expert Advice

HomeNewsTopicsTipsAsk the ExpertsMultimediaWhite PapersProductsBlogs
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides enterprise IT professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective IT purchase decisions and managing their organizations' IT projects - with its network of technology-specific Web sites, events and magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Reprints  |  Site Map




All Rights Reserved, Copyright 2003 - 2008, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts