bimalpoudel — 2011-01-23T13:40:06-05:00 — #1
I have a special case of capturing by regular expression in the SELECT statement.
This is the background: In a poorly designed database, a user might have stored the
amount as the following record:
I would like to capture the data with the digits and a period only.
And all the fields should be having same output in this case as:
My ideal query would be something like:
amount REGEXP '/[\\d\\.]+/is' AS amount
What can be the real alternative to this problem?
It is not a matching for a condition, but the data extraction:
extracting the digits and a period.
By this way, I can even sort the records numerically (low to high, or high to low) with results as expected.
Here were some other issues on regular expressions, a bit different than mine:
Thank you for your time in searching for a solution.
bimalpoudel — 2011-01-23T13:42:01-05:00 — #2
Further, CAST is not a solution.
I don't want to use PHP calculations, because I need to sort the data at the query level.
r937 — 2011-01-23T14:55:19-05:00 — #3
since mysql's regexp can find strings but cannot replace them, the best you can do in the query is...
bimalpoudel — 2011-01-23T15:19:37-05:00 — #4
Poor database design --> poor solution. I really agree with you!
This was one of the real life case I have ever seen.
Even something like this in my mind:
Build all possible non-numerical records in each rows.
DISTINCT them in a temp zone.
Build the list of REPLACE dynamically.
Build the full SQL.
Just because, you cannot predict what could have been stored in the VARCHAR column.
Sometimes, we have to do the wrong way though we know it.
furicane — 2011-01-23T16:36:12-05:00 — #5
Do you have the option to convert this poor design to a better one or is it one of those "do not change anything, but please make it work" projects? I would promptly upgrade this varchar to NUMERIC storage (I'm not saying I'd just change the data type of the column of course, but do a proper re-import of the data).
bimalpoudel — 2011-01-23T23:41:17-05:00 — #6
No rights to modify the data/database.
Option to re-import really helps; but not allowed.