1

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

Saharsh Shah
  • 28,687
  • 8
  • 48
  • 83
Khaldonno
  • 13
  • 1
  • 4

5 Answers5

5

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:]]+$'
Saharsh Shah
  • 28,687
  • 8
  • 48
  • 83
0

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).

BaBL86
  • 2,602
  • 1
  • 14
  • 13
0

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

Community
  • 1
  • 1
Charaf JRA
  • 8,249
  • 1
  • 34
  • 44
0

Try something like this. It works for me

if ('value_to_check' REGEXP '^[0-9]+$') {
THEN 
// Do ur work,..
}
Lakshmi
  • 2,204
  • 3
  • 29
  • 49
-2

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

WHERE ColumnA REGEXP '[0-9]+'
Yogesh Gupta
  • 170
  • 1
  • 15