SQL Sysdate Decode Statement EASY POINTS

Posted on 2005-05-16
Last Modified: 2008-02-01
hi i have this statment


essentially i wanted to operate like this, If the day is Monday, then sysdate = sysdate - 3
else sysdate = sysdate - 1

what i have here works fine on every other day except monday

could someone explain why? does it have something to do with the way its formatted?

can you provide an example of what might be a better solution

Question by:daveleblanc
    LVL 11

    Accepted Solution

    I hope this explains it:
    SQL > select to_char(sysdate,'dy') from dual;


    SQL > select to_char(sysdate,'Dy') from dual;


    The "Mon" you're comparing to in the decode starts with an upper case "M", that's why it doesn't match. Your format mask needs to be 'Dy', or if you leave it as 'dy' the string you compare to needs to be 'mon' (instead of 'Mon').
    Hope this helps!
    LVL 7

    Expert Comment

    You can use:

                     WHEN UPPER(TO_CHAR(SYSDATE, 'dy')) = 'MON' THEN SYSDATE-3
                     ELSE SYSDATE-1

    Share and Enjoy  Christoph
    LVL 34

    Expert Comment

    I would use something like this to get down to just one call to sysdate:

    select case to_char(sysdate,'dy') when 'mon' then -3 else -1 end "Adj" from dual;

    To use this in PL\SQL, just replace "sysdate" with your date column or variable and drop the alias "Adj" and the "from dual".
    LVL 34

    Expert Comment

    Just remember that unless you are on Oracle10, each call to sysdate causes at least one logical block read.  If this is in a procedure that gets exectuted frequently, those multiple calls to sysdate can add up to a noticeable performance impact.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    PRTG Network Monitor: Intuitive Network Monitoring

    Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

    Working with Network Access Control Lists in Oracle 11g (part 2) Part 1: Previously, I introduced the basics of network ACL's including how to create, delete and modify entries to allow and deny access.  For many…
    I remember the day when someone asked me to create a user for an application developement. The user should be able to create views and materialized views and, so, I used the following syntax: (CODE) This way, I guessed, I would ensure that use…
    This video shows setup options and the basic steps and syntax for duplicating (cloning) a database from one instance to another. Examples are given for duplicating to the same machine and to different machines
    Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now