title: How to conditionally replace a date with another in SQL?
body: <p>I have a dataset that has lots of date ranges. I want to build a code that just lets me select, say, days in the past 30 days. However, i want to truncate or exclude the days before the start of my range.</p>
  
  <p>Let's imagine that my dataset is this:</p>
  
  <pre><code>StayID StayStart StayEnd
  1      Jan 1     Jan 10
  2      Jan 1     Feb 28
  3      Feb 1     Feb 10
  4      Feb 10    Feb 28
  </code></pre>
  
  <p>I also have a parameter {?ReportStart} and let's say I set it to Feb 1. First I would exclude all Stays that are before then, but I also want to modify the dates so that all dates are after the selected range. I want the following output:</p>
  
  <pre><code>StayID StayStart StayEnd
  2      Feb 1     Feb 28
  3      Feb 1     Feb 10
  4      Feb 10    Feb 28
  </code></pre>
  
  <p>I'm thinking I should do something like this, but I'm very new to SQL:</p>
  
  <pre><code>SELECT StayID, 
  (SELECT CASE WHEN StayStart &lt; {?ReportStart} THEN {?ReportStart}
      ELSE StayStart
      END
      ) AS StayStart,
  StayEnd
  </code></pre>
  
  <p>Am I on the right track? Any advice would be appreciated!</p>