ORACLE验证字段值是否为数字

--创建测试表
DROP TABLE TEST_TO_NUMBER;
CREATE TABLE TEST_TO_NUMBER
(
    ID VARCHAR2(2) PRIMARY KEY,
    TEST_VAL VARCHAR2(20) NOT NULL
);
--插入测试数据
INSERT INTO TEST_TO_NUMBER (ID, TEST_VAL) VALUES ('1','123');
INSERT INTO TEST_TO_NUMBER (ID, TEST_VAL) VALUES ('2','0.12');
INSERT INTO TEST_TO_NUMBER (ID, TEST_VAL) VALUES ('3','2.141');
INSERT INTO TEST_TO_NUMBER (ID, TEST_VAL) VALUES ('4','.0.1');
INSERT INTO TEST_TO_NUMBER (ID, TEST_VAL) VALUES ('5','a.213');
INSERT INTO TEST_TO_NUMBER (ID, TEST_VAL) VALUES ('6','1..2');
INSERT INTO TEST_TO_NUMBER (ID, TEST_VAL) VALUES ('7','1a1');

--创建验证方法
CREATE OR REPLACE FUNCTION IS_NUMBER (IN_DATA IN VARCHAR2, --原始数据
                                      I IN NUMBER, --整数位数最大允许值
                                      J IN NUMBER  --小数位数最大允许值 0-不能有小数位 ,-1 不校验小数长度
                                      )
   RETURN INTEGER
AS
   V_NUMBER    NUMBER;
   V_XIAOSHU   VARCHAR2 (128);
--数据类型校验 返回1 :正确 返回0: 错误
BEGIN
   IF (INSTR (IN_DATA,
              ',',
              1,
              1) <> 0)
   THEN
      RETURN 0;
   END IF;

   IF J = -1
   THEN
      V_XIAOSHU := '9999999999999999999999999999';
   ELSIF J = 0
   THEN
      V_XIAOSHU := '0';
   ELSE
      V_XIAOSHU := TRIM (LPAD (' ', J + 1, '9'));
   END IF;

   V_NUMBER :=
      TO_NUMBER (IN_DATA, TRIM (LPAD (' ', I + 1, '9')) || '.' || V_XIAOSHU); --利用类型转换异常判断是否合法
   RETURN 1;
EXCEPTION
   WHEN OTHERS
   THEN
      RETURN 0;
END IS_NUMBER;

--查询不是数字的字段
SELECT * FROM (SELECT ID,IS_NUMBER(TEST_VAL,20,20) A FROM TEST_TO_NUMBER) WHERE A = '0';

你可能感兴趣的:(数据库工具,配置,问题等)