Results 1 to 2 of 2

Thread: Numbers with commas in database

  1. #1
    Join Date
    Nov 2006
    Posts
    249

    Numbers with commas in database

    I have a mysql database set up and one of the rows has numbers like "34,000".

    I want to pull the info that is less than 20,000. What would be the best way to do that?

    If the commas were not there it would be as simple as "Where $number > 20000". but with the commas it seems a little trickier.

    Thanks

  2. #2
    Join Date
    Feb 2007
    Location
    Ireland
    Posts
    1,007
    Quote Originally Posted by Magiccupcake View Post
    I have a mysql database set up and one of the rows has numbers like "34,000".

    I want to pull the info that is less than 20,000. What would be the best way to do that?

    If the commas were not there it would be as simple as "Where $number > 20000". but with the commas it seems a little trickier.

    Thanks
    3 methods off the top of my head.

    1. Use the explode function to break it into two elements of an array and then glue it back together with the implode function.

    PHP Code:
    $number implode("",explode(",",$number)); 
    2. You could replace the "," with nothing.

    PHP Code:
    str_replace(",","",$number); 
    3. You could use a custom function to search for the "," and remove it.

    PHP Code:
    function removeComma($string)
    {
       
    $newstr '';
       
    $length strlen($string);
       for(
    $i=0;$i<$length;$i++)
       {
          
    $char substr($i,1,$string);
          if(
    $char != ",")
             
    $newstr .= $char;
       }
       return 
    $newstr;

    “The best thing about a boolean is even if you are wrong, you are only off by a bit.”

Similar Threads

  1. database download automation
    By brokenshadows in forum Client & Server Side Scripting (PHP, ASP, JavaScript)
    Replies: 1
    Last Post: 07 Jul 2007, 09:07 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •