0

value->{1,2,Yes,No,5,6}

select if((value is numeric),value,'not a numeric') as column_name 

how to implement this if in my mysql select query

1

3 Answers 3

6

This should do it :)

select if(field REGEXP '^-?[0-9]+$' > 0, field, 'not a numeric') as column_name

Example:

SELECT '12345' REGEXP '^-?[0-9]+$'  

Returns: 1 (its a number)

SELECT 'abcdef' REGEXP '^-?[0-9]+$'  

Returns: 0 (its NOT a number)

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

Comments

5

This is an old question, but when I needed the same thing the fastest solution was

select if((value *1),value, 'not a number') as column_name;

Granted, this will not consider zero a number, but neither did the Romans and they did ok for thousands of years. For me, given the improved speed across the hundreds of millions of rows, this was the best solution.

1 Comment

Neat, thanks. In my case I wanted to group by, summing a column if that value was valid. so sum(if(value*1,value,0)) as "Sum" worked fine.
0

You can try it-

SELECT IF(table_column*1>0,table_column,'Not a Numeric') 
FROM your_table;

Note: If there is a value "9 hello" then it will be treat as numeric as 9 is numeric value, if you need to exclude it also then revert. But in your question there is no such value.

Comments

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.