title: MySQL end SUBSTRING with sentence
body: <p>Here is my example. When I run this query:</p>
  
  <pre><code>SELECT SUBSTRING('Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec vitae odio tellus, sit amet facilisis neque. Cras sapien nulla, suscipit non imperdiet ac, aliquet vitae lectus. Nulla a varius dolor. Nunc convallis, augue et fermentum volutpat, lectus urna sagittis risus, vel iaculis diam est convallis lectus. Suspendisse potenti. Morbi malesuada tristique varius.', 1, 300)
  </code></pre>
  
  <p>I get the first 300 chars of the sentence. That is good. Except that I want it to end where the last dot (.) is in the result.</p>
  
  <p>So the query result is this:</p>
  
  <pre><code>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec vitae odio tellus, sit amet facilisis neque. Cras sapien nulla, suscipit non imperdiet ac, aliquet vitae lectus. Nulla a varius dolor. Nunc convallis, augue et fermentum volutpat, lectus urna sagittis risus, vel iaculis diam est convalli
  </code></pre>
  
  <p>But I want it to be this:</p>
  
  <pre><code>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec vitae odio tellus, sit amet facilisis neque. Cras sapien nulla, suscipit non imperdiet ac, aliquet vitae lectus. Nulla a varius dolor.
  </code></pre>
  
  <p>Can I do that with a MySQL query, and how?</p>
  
  <p>Tnx</p>
  
  <hr>
  
  <p>UPDATE 1,
  This is what I had so far:</p>
  
  <pre><code>SET @longtext = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec vitae odio tellus, sit amet facilisis neque. Cras sapien nulla, suscipit non imperdiet ac, aliquet vitae lectus. Nulla a varius dolor. Nunc convallis, augue et fermentum volutpat, lectus urna sagittis risus, vel iaculis diam est convallis lectus. Suspendisse potenti. Morbi malesuada tristique varius.';
  
  SELECT SUBSTRING_INDEX(
      @longtext,
      SUBSTRING_INDEX(
          SUBSTRING(
              @longtext,
              1,
              300
          ),
          '.',
          -1
      )
      , 1
  );
  </code></pre>
  
  <hr>
  
  <p>UPDATE 2,
  Tnx @Ben</p>
  
  <p>My solution is:</p>
  
  <pre><code>SET @longtext = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec vitae odio tellus, sit amet facilisis neque. Cras sapien nulla, suscipit non imperdiet ac, aliquet vitae lectus. Nulla a varius dolor. Nunc convallis, augue et fermentum volutpat, lectus urna sagittis risus, vel iaculis diam est convallis lectus. Suspendisse potenti. Morbi malesuada tristique varius.';
  
  SELECT SUBSTRING(
      @longtext,
      1,
      300 - ifnull(
          LOCATE(
              '.',
              REVERSE(
                  SUBSTRING(
                      @longtext,
                      1,
                      300
                  )
              )
          ),
      1)
  );
  </code></pre>