1

Sometimes we got raw data of number is imported in a human readable string format, e.g. "955.37K". In Mysql, is there any built-in method to convert it back to computable types, e.g. 955370.0 ?

1 Answer 1

1

You could use a CASE expression with the help of regex substring logic:

SELECT
    data,
    CASE REGEXP_SUBSTR(data, '[A-Z]$')
         WHEN 'K' THEN 1000
         WHEN 'M' THEN 1000000
         WHEN 'B' THEN 1000000000
         ELSE 1 END *
    CAST(REGEXP_SUBSTR(data, '^\\d+(\\.\\d+)?') AS UNSIGNED) AS full_data
FROM yourTable;

Demo

Sign up to request clarification or add additional context in comments.

1 Comment

thanks, @Tim. hope it's supported in mysql cos it's similar to datetime handling. but if not we definitely can use this approach.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.