title: SQLAlchemy - min/max between value and the default
body: <p>I'm trying to prepare a hybrid expression for such python query:</p>
  
  <pre><code>(min(end_date, self.start.date()) - max(start_date, self.end.date())).days
  </code></pre>
  
  <p>Basically I want to calculate how many days from given period are between object's start and end. An easier example written with ints. Let's say we have an object with start and end set to 3 and 8. Given such values for input it should return:</p>
  
  <ul>
  <li>1, 10 - return 5 (distance between 3 and 8)</li>
  <li>5, 10 - return 3 (distance between 5 and 8)</li>
  <li>1, 6 - return 3 (distance between 3 and 6)</li>
  <li>5, 6 - return 1 (distance between 5 and 6)</li>
  </ul>
  
  <p>Is there any other way to write in in sqlalchemy other than giant case statement for each option?</p>