我怎样才能知道哪些值是在Oracle 9i数字
题
我有此数据库包含一个varchar。
我想知道哪些记录保存数值。我试图REGEXP_COUNT
等,但我在9i的运行,我认为这是的10G>
我怎样才能做到这一点?
我尝试:
select to_number( my_column ) from my_table
但是,这是行不通的,因为他们的好话不是所有的都是数字。
修改强>
背景
此表包含雇员的ID,所有这些都是数字(读或1234或24523 6655)
在初始数据库负荷,当雇员ID是未知的,而不是使用类似-1
他们进入像文本:
NA, N/A, NONE, UNK, UNKNOW, TEST, EXTERNAL, WITHOUT_ID
真的主故障,该列是varchar
而不是号码,它应。
现在,我尝试做,是让LL非数值(不包含雇员ID)的记录,但因为这是数据库9i中,我无法使用正则表达式
解决方案 6
我设法解决这样的:
select my_column
from my_table
where my_column not like '%1%'
and my_column not like '%2%'
and my_column not like '%3%'
and my_column not like '%4%'
and my_column not like '%5%'
and my_column not like '%6%'
and my_column not like '%7%'
and my_column not like '%8%'
and my_column not like '%9%'
and my_column not like '%0%'
脏,但它工作。 ;)
其他提示
在这个
恐怕你必须写自己的ISNUMBER功能,然后使用它,像这样(未经测试)中发现的螺纹时,应该工作。
DECLARE FUNCTION isNumber(p_text IN VARCHAR2) RETURN NUMBER IS
v_dummy NUMBER;
not_number EXCEPTION;
PRAGMA EXCEPTION_INIT(-, not_number);
BEGIN
v_dummy := TO_NUMBER(p_text);
RETURN 1;
EXCEPTION
WHEN not_number THEN RETURN 0;
END is_number;
之后,你可以使用解码功能与您的ISNUMBER功能相结合,让你需要的结果。
又一个纯SQL解决方法:
select my_column
from my_table
where translate(my_column,'x0123456789','x') is null;
取决于你算什么为“数字”。你让负数,小数或只是整数或科学记数法(如“1E3”)。是导致零允许吗?
如果你只是想正整数,尝试
where translate(col,' 1234567890','0') is null
尝试此
CREATE OR REPLACE PACKAGE value_tests
AS
FUNCTION get_number( pv_value IN VARCHAR2 ) RETURN NUMBER;
END;
/
CREATE OR REPLACE PACKAGE BODY value_tests
AS
FUNCTION get_number( pv_value IN VARCHAR2 ) RETURN NUMBER
IS
converted_number NUMBER;
invalid_number EXCEPTION;
PRAGMA EXCEPTION_INIT( invalid_number, -01722 );
value_error EXCEPTION;
PRAGMA EXCEPTION_INIT( value_error, -06502 );
BEGIN
<<try_conversion>>
BEGIN
converted_number := TO_NUMBER( pv_value );
EXCEPTION
WHEN invalid_number OR value_error
THEN
converted_number := NULL;
END try_conversion;
RETURN converted_number;
END get_number;
END;
/
在此运行它...
select my_column
, value_tests.get_number( my_column ) my_column_num
from ( select 'mydoghas3legs' my_column from dual
union all select '27.5' my_column from dual
union all select '27.50.5' my_column from dual
)
返回
MY_COLUMN MY_COLUMN_NUM
------------- -------------
mydoghas3legs
27.5 27.5
27.50.5
我不喜欢在正常的代码中使用异常,但,这似乎是最好的和最安全的形式给出了:
CREATE OR REPLACE FUNCTION "IS_NUMBER" (pX in varchar2) return integer is
n number;
begin
n:=to_number(pX);
return 1;
exception
when others then
return 0;
end;
另一种方法,这里是我写了前一段时间的函数:
CREATE OR REPLACE function string_is_numeric
(p_string_in in varchar2)
return boolean is
begin
for i in 1..length(p_string_in) loop
if substr(p_string_in, i, 1) not in ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9') then
return false;
end if;
end loop;
return true;
end;
/
作为pedromarce的答复中指出,可能需要改变,从一个布尔值回数或VARCHAR2以更好地满足您的需求。
下面是我的常规,类似于张贴pedromarce的版本。请注意,例如张贴不编译由于“ - ”异常号。本实施例中编译和工作的:
create or replace function IsNumber(
a_Text varchar2
) return char is
t_Test number;
begin
begin
t_Test := to_number(a_Text);
return 'Y';
exception when value_error then
return 'N';
end;
end;
用法示例:
select IsNumber('zzz') from dual;
结果:N
select IsNumber('123.45') from dual;
结果:Y
不隶属于 StackOverflow