Incrementing numerical part of an alphanumerical value

Posted on 2007-08-03
Last Modified: 2013-12-12

I have existing alphanumeric values in a mysql db field that is of the following format:


some examples:


the "111" can be anything from just "1" to "99999", while the "XYZ" is fixed to 3 alpha characters, and the "22" is anything between "01 and 12 - (months)"

I want to first of all determine the largest "111" value that is currently in the DB, then increment it by 1, append a $_POST var containing the alpha values and then append the last two digits to form a new alpha string.

I'm not real good with regexp etc, so I'm battling with the query as well as "extracting" just the first numerical part so I can increment it.

Question by:psimation
    1 Comment
    LVL 13

    Accepted Solution

    $query = "SELECT Value FROM Table ORDER BY Value DESC LIMIT 1";
    $value = mysql_fetch_array(mysql_query($query));
    $value = $value[0];

    $next_num = intval(substr($value,0,3))+1;
    $new_value = $next_num.$_POST['alpha'].$month;

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    This is a general how to create your own custom plugin system for your PHP application that you designed (or wish to extend a third party program to have plugin functionality that doesn't have it yet).  This is not how to make plugins for existing s…
    Part of the Global Positioning System A geocode ( is the major subset of a GPS coordinate (, the other parts being the altitude and t…
    The viewer will learn how to dynamically set the form action using jQuery.
    The viewer will learn how to count occurrences of each item in an array.

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now