Mikhail Sidorov
Mikhail Sidorov

Reputation: 799

SQL - select all rows from table where a column contains only digits

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

Upvotes: 1

Views: 1852

Answers (1)

Pரதீப்
Pரதீப்

Reputation: 93754

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]+$'

Upvotes: 1

Related Questions