Sql 交叉dbms检查字符串是否为数字的方法

Sql 交叉dbms检查字符串是否为数字的方法,sql,mysql,oracle,Sql,Mysql,Oracle,好的,我有这个字段:code varchar(255)。它包含导出例程中使用的一些值,如 DB84 DB34 3567 3568 我只需要选择自动生成的(全数字)字段 is\u numeric()检查code字段是否仅包含正数 您能提出任何在mysql 5.1和oracle 10g下都能工作的方案吗?下面是针对SQL Server、mysql和oracle的三种不同实现。没有人使用(或可以)相同的方法,因此似乎没有一种跨DBMS的方法可以做到这一点。 对于MySQL和Oracle,只显示简单的

好的,我有这个字段:
code varchar(255)
。它包含导出例程中使用的一些值,如

DB84
DB34
3567
3568
我只需要选择自动生成的(全数字)字段

is\u numeric()
检查
code
字段是否仅包含正数


您能提出任何在mysql 5.1和oracle 10g下都能工作的方案吗?

下面是针对SQL Server、mysql和oracle的三种不同实现。没有人使用(或可以)相同的方法,因此似乎没有一种跨DBMS的方法可以做到这一点。 对于MySQL和Oracle,只显示简单的整数测试;对于SQL Server,将显示完整的数字测试

对于SQL Server: 请注意,isnumeric('.')返回1。。但它实际上不能转换为float。某些文本(如“1e6”)无法直接转换为数字,但可以先传递浮点,然后传递数字

对于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
甲骨文

case when REGEXP_LIKE(col, '[^0-9]') then col*1 end

您是否需要担心诸如
-
之类的字符,还是只对包含所有数字的数字感兴趣?(即正整数)编辑:我不确定这是否重要实际上我认为
不像“[^0-9]%”
,但这很可能是SQL Server特有的:-(猜测一些标准SQL-92,例如
CAST(代码为数字(x,y))
具有适用于
x
y
的值。我只对包含所有数字而不包含任何内容的字符串感兴趣more@jonny-如果您仅在数字之后,则我已使用更简单的test@cyberwiki:虽然像这样的
是标准SQL,但对正则表达式的支持不是标准的,不能与o一起使用其他数据库(Oracle、Postgres、Firebird、DB2)。我认为MySQL也不支持它,但我不确定这一点。@a_horse_和_no_name-我已经知道LIKE不是REGEXP。MySQL和Oracle有SQL Server不支持的特定REGEXP支持。LIKE只是SQL,而“%”几乎不是REGEXP,因为“*”是“%”所代表的regexp。我只是想避免混淆您的示例将在哪里工作。您在MySQL和Oracle中编写了LIKE[…]work`这是正确的,只是不是您在示例中使用它的方式。
LIKE“[^0-9e.+-]%”
使用了regex“range”这部分只适用于SQL Server。您应该添加第三项,内容为“*删除示例中正则表达式范围的使用”。@a_h您是对的。答案已更新。开始在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
case when REGEXP_LIKE(col, '[^0-9]') then col*1 end