title: Oracle Date Format Mystery - Why is it not an acceptable format?
body: <p>I am fairly new to SQL but have still written a few queries that are very similar to the one I am writing now. For whatever reason when I run this query I am returned an 'ORA-01821: date format not recognized' error. I looked this up and looked around here on Stack and other places and I believe my syntax does make sense so I am confused as to why I'm thrown an error.</p>
  
  <p>My query runs based on what day of the month it is. If it is the 1st, it should run for the 15th-last day of the previous month. If it is the 16th, it should run for the 1st-15th of the current month.</p>
  
  <p>This is my code for when it is the first day of the month:</p>
  
  <pre><code>select 
  case
     when to_char(sysdate, 'yyyymmdd') = to_char(sysdate, 'yyyymm' || '01') then (do a lot of things)
     .
     .
     .
  end as FirstReportGroup
  where *datetable* between between to_char(sysdate, 'yyyymm' || '16') and to_char(last_day(sysdate), 'yyyymmdd');
  </code></pre>
  
  <p>and this is my code for when it is the 16th:</p>
  
  <pre><code>select 
  case
     when to_char(sysdate, 'yyyymmdd') = to_char(sysdate, 'yyyymm' || '16') then (do a lot of things again)
     .
     .
     .
  end as SecondReportGroup
  where *datetable* between to_char(sysdate, 'yyyymm' || '01') and to_char(sysdate, 'yyyymm' || '15')
  </code></pre>
  
  <p>It must be some kind of date formatting syntax error that I'm just not seeing. I would really appreciate some help here and I look forward to figuring this out! Let me know if you need more info.</p>
  
  <p>Thanks.</p>