1

I want to find all rows in which a column contains only digits.

The first idea was using LIKE [0-9]%, but in fact this matches any strings starting with a digit.

It seems to be a very simple task, but unfortunately I can't find solution

Mikhail Sidorov
  • 699
  • 2
  • 9
  • 18
  • 2
    What [tag:rdbms] are you using? – Mureinik Jan 03 '16 at 09:14
  • If it's MS SQL 2005 or later, I believe you will require support for regular expressions (or *regex*) to acheive your goal. You will need to write a CLR function. A (slightly dated) example is here: http://www.c-sharpcorner.com/uploadfile/f9935e/harnessing-regular-expressions-in-sql-server-using-clr-integration/ – ne1410s Jan 03 '16 at 09:22
  • They say mySQL 5.5 is used. But if i'm not mistaken "REGEXP" is possible in mySQL, but in my case it doesn't work – Mikhail Sidorov Jan 03 '16 at 09:33
  • If you're using mySQL try checking [this](https://stackoverflow.com/questions/5064977/detect-if-value-is-number-in-mysql) – Serpiton Jan 03 '16 at 09:58

1 Answers1

1

In Sql server use Not Like

where some_column NOT LIKE '%[^0-9]%'

Demo

declare @str varchar(50)='asdarew345'

select 1 where @str NOT LIKE '%[^0-9]%' 

For Mysql use REGEX

SELECT * 
FROM yourtable 
WHERE string_column REGEXP '^[0-9]+$'
Pரதீப்
  • 91,748
  • 19
  • 131
  • 172