Question

Is there a way to update a column in mysql if value is number only?

ie. UPDATE myTable SET ColumnA =NewValue WHERE ColumnA isnumeric

Column A is varchar(25) and Column A value may be text or 1 or 2 or 23...

thank u

Was it helpful?

Solution 5

Use regular expression to see if your column contains number or anything else

WHERE ColumnA REGEXP '[0-9]+'

OTHER TIPS

Use REGEXP() function

Try this:

UPDATE myTable SET ColumnA = NewValue WHERE ColumnA REGEXP '^[0-9]+$'

OR

UPDATE myTable SET ColumnA = NewValue WHERE ColumnA REGEXP '^[[:digit:]]+$'

Try to create INT column and make call:

update myTable set IntColumn = ColumnA

than replace intcolumn type to varchar and update your table where ColumnA = IntColumn (Not real numeric strings will be empty or different from real numeric).

Try this :

UPDATE myTable SET ColumnA =NewValue WHERE concat('',ColumnA * 1) = ColumnA 

Or

UPDATE myTable SET ColumnA =NewValue WHERE ColumnA REGEXP '[0-9]+';

Look at this topic Detect if value is number in MySQL

Try something like this. It works for me

if ('value_to_check' REGEXP '^[0-9]+$') {
THEN 
// Do ur work,..
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top