 <p>Maybe I'm missing something in your question, but it seems that this should do it:</p> <pre><code>SELECT CONCAT('STK', LPAD(MAX(SUBSTRING(ticket_id, 4)) + 1, 10, '0') ) FROM sma_support_tickets; </code></pre>