The following are three separate implementations for each SQL Server, MySQL, and Oracle. None of them use (or can) use the same approach, so there seems to be no cross-DBMS method for this. For MySQL and Oracle, only a simple integer test is shown; For SQL Server, a complete numerical test is shown.
For SQL Server: note that isnumeric ('.') Returns 1 .. but it cannot actually be converted to float. Some text, such as "1e6", cannot be converted to a numeric number directly, but you can go through a float, then a numeric one.
;with tmp(x) as ( select 'db01' union all select '1' union all select '1e2' union all select '1234' union all select '' union all select null union all select '1.2e4' union all select '1.e10' union all select '0' union all select '1.2e+4' union all select '1.e-10' union all select '1e--5' union all select '.' union all select '.123' union all select '1.1.23' union all select '-.123' union all select '-1.123' union all select '--1' union all select '---1.1' union all select '+1.123' union all select '++3' union all select '-+1.123' union all select '1 1' union all select '1e1.3' union all select '1.234' union all select 'e4' union all select '+.123' union all select '1-' union all select '-3e-4' union all select '+3e-4' union all select '+3e+4' union all select '-3.2e+4' union all select '1e1e1' union all select '-1e-1-1') select x, isnumeric(x), case when x not like '%[^0-9]%' and x >'' then convert(int, x) end as SimpleInt, case when x is null or x = '' then null
For MySQL
create table tmp(x varchar(100)); insert into tmp select 'db01' union all select '1' union all select '1e2' union all select '1234' union all select '' union all select null union all select '1.2e4' union all select '1.e10' union all select '0' union all select '1.2e+4' union all select '1.e-10' union all select '1e--5' union all select '.' union all select '.123' union all select '1.1.23' union all select '-.123' union all select '-1.123' union all select '--1' union all select '---1.1' union all select '+1.123' union all select '++3' union all select '-+1.123' union all select '1 1' union all select '1e1.3' union all select '1.234' union all select 'e4' union all select '+.123' union all select '1-' union all select '-3e-4' union all select '+3e-4' union all select '+3e+4' union all select '-3.2e+4' union all select '1e1e1' union all select '-1e-1-1'; select x, case when x not regexp('[^0-9]') then x*1 end as SimpleInt from tmp order by 2
For Oracle
case when REGEXP_LIKE(col, '[^0-9]') then col*1 end
source share