Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First some date manipulation examples without converting everything to and back from strings.</p> <pre><code>SELECT SYSDATE - INTERVAL '1' YEAR -- returns the previous year from current date , TRUNC( SYSDATE, 'MM') -- returns the first day of the current month , ADD_MONTHS( TRUNC( SYSDATE, 'YYYY'), 11) -- returns December of current year , ADD_MONTHS( TRUNC( SYSDATE - INTERVAL '1' YEAR, 'YYYY'), 11) -- returns first day of december previous year , DECODE( TRUNC(SYSDATE, 'MM') , ADD_MONTHS(TRUNC(SYSDATE, 'YYYY'), 11), TRUNC( SYSDATE, 'MM' ) , ADD_MONTHS(TRUNC(SYSDATE, 'YYYY' ), 11) - INTERVAL '1' YEAR ) -- this implements your logic from dual </code></pre> <p>The last decode returns December 1st 2010 if the current month is NOT December 2011. If You run it on December 2011 it will return the 1st of December 2011.</p> <p>Now depending on how you want your date ranges you can use the following statements:</p> <p>If the date ranges are INCLUSIVE</p> <pre><code>SELECT e.* FROM EMPLOYEE e WHERE e.employee_timestamp BETWEEN DECODE( TRUNC(SYSDATE, 'MM') , ADD_MONTHS(TRUNC(SYSDATE, 'YYYY'), 11), TRUNC( SYSDATE, 'MM' ) , ADD_MONTHS(TRUNC(SYSDATE, 'YYYY' ), 11) - INTERVAL '1' YEAR ) AND SYSDATE </code></pre> <p>If the date ranges are exactly like you specified (EXCLUSIVE)</p> <pre><code> SELECT e.* FROM EMPLOYEE e WHERE e.employee_timestamp &gt; DECODE( TRUNC(SYSDATE, 'MM') , ADD_MONTHS(TRUNC(SYSDATE, 'YYYY'), 11), TRUNC( SYSDATE, 'MM' ) , ADD_MONTHS(TRUNC(SYSDATE, 'YYYY' ), 11) - INTERVAL '1' YEAR ) AND e.employee_timestamp &lt; SYSDATE </code></pre> <p>Hope this answers your question :) Remember to document this, code like this is hard to analyse years later.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload