Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4022704
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:30:32+00:00 2026-05-20T10:30:32+00:00

I’m working on updating a system that stores financial information and am using a

  • 0

I’m working on updating a system that stores financial information and am using a table structure that uses DECIMAL fields for the data in question.

Unfortunately, my predecessor, in his/her Infinite Wisdom implemented the fields in the old database as varchar. The amount of data input validation that was done also seems to have been light, to put it kindly, and there’s all kind of junk data in there. Some fields store the value NaN, some store values formatted as 1,234,567.89, some store values formatted as 1.234.567.89, some store 1234567.89, some include currency symbols at the end, some include currency symbols in the middle, some even contain sums! (123 + 456 for example).

Obviously, casting as DECIMAL can only help with some of these. In cases where the first character isn’t numeric I’m going to get 0 back. Worse, in cases where there are commas or more than one decimal point in a number I’m going to get an incorrect result back.

I need some way of massaging the data into a more useful form, as such:

  • 1234567.89 -> 1234567.89 (simply casting will work here)
  • 1234567.89$ -> 1234567.89 (Casting these seems to give the correct result)
  • £1234567.89 -> 1234567.89 (Casting returns 0)
  • 1,234,567,89 -> 1234567.89 (Casting here returns 1)
  • 1.234.567.89 -> 1234567.89 (Casting gives 1.234)
  • 123 + 456 -> 579.00 (No freaking idea how I’m going to deal with these)
  • NaN or other non-numerical data -> 0 (No sensible way of dealing with these, so just inserting 0 will have to do)

I will also, naturally, have to be able to deal with cases with multiple faults, such as $1,234.567.89.

I’m thinking Regex is the only option here, but as far as I can tell, MySQL only provides regex matching, it doesn’t seem to have any regex replacement features.

If you could help with this I’d really appreciate it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T10:30:32+00:00Added an answer on May 20, 2026 at 10:30 am

    I feel there’s no sensible way of doing this without resorting to a scripting language, so I’ve written the following PHP code to address the problem.

    function notEmptyString ($val)
    {
        return ($val !== '');
    }
    
    /**
     * Make an attempt at extracting menaingful numeric data from a string that can contain all kinds of garbage
     * @param string $string
     * @return int
     */
    function mungeNumber ($string)
    {
        $num    = 0;
        if (($digits = preg_split ('/[^0-9]/', $string))
        && ($digits = array_filter ($digits, 'notEmptyString')))
        {
            $decimal    = (count ($digits) > 1)? array_pop ($digits): 0;
            $num        = (implode ('', $digits) . '.' . $decimal) * 1;
        }
        return ($num);
    }
    

    So far it seems to have coped with all the test data I’ve given it, though I’m still coming up with some more suitably pathological test cases for it to cope with. I know for a fact that it won’t deal with the cases where the value seems to be a sum, but i don’t think there’s a great deal I can do about that, and the times where that seems to be the case are mercifully small.

    When a field contains two or more distinct numbers the result will be a single number, which is unfortunate. However, the numbers in question will be excessively large compared to the others in the set so should be easy to spot and deal with manually.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working with an upstream system that sometimes sends me text destined for HTML/XML
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I
I am reading a book about Javascript and jQuery and using one of the
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.