2011-01-14 89 views
4

好吧,我有這個字段:code varchar(255)。它包含了我們的出口程序中使用像cross-dbms方法來檢查字符串是否是數字

DB84 
DB34 
3567 
3568 

一些值,我需要選擇只自動生成的(全數字)字段

WHERE is_numeric(table.code) 

is_numeric()檢查code字段只包含積極的數字。

你能提出什麼可以在mysql 5.1和oracle 10g下使用嗎?

+1

你擔心的字符,如`.`和`-`還是你只關心包含所有號碼數字? (即正整數)編輯:我不確定它事實上我在想`不像'%[^ 0-9]%'`但看起來像這可能是特定於SQL Server :-( – 2011-01-14 09:28:45

+1

猜測一些標準的SQL -92例如`CAST(代碼爲AS NUMERIC(x,y))`,其中'x`和`y`的值合適 – onedaywhen 2011-01-14 10:14:14

回答

4

下面是SQL Server,MySQL和Oracle中每一個的三個獨立實現。沒有人使用(或可以)使用相同的方法,所以似乎沒有跨DBMS的方式來做到這一點。 對於MySQL和Oracle,只顯示簡單整數測試;對於SQL Server,將顯示完整的數字測試。

對於SQL Server: 注意isnumeric('。')返回1 ..但它實際上不能轉換爲float。像「1e6」這樣的文本不能直接轉換爲數字,但可以通過float,然後通過數字。

;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 -- blanks 
    when x like '%[^0-9e.+-]%' then null -- non valid char found 
    when x like 'e%' or x like '%e%[e.]%' then null -- e cannot be first, and cannot be followed by e/. 
    when x like '%e%_%[+-]%' then null -- nothing must come between e and +/- 
    when x='.' or x like '%.%.%' then null -- no more than one decimal, and not the decimal alone 
    when x like '%[^e][+-]%' then null -- no more than one of either +/-, and it must be at the start 
    when x like '%[+-]%[+-]%' and not x like '%[+-]%e[+-]%' then null 
    else convert(float,x) 
    end 
from tmp order by 2, 3 

對於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 

對於Oracle

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