简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM