首页 » ORACLE 9i-23c » sql varchar字段中是数字的

sql varchar字段中是数字的

如果一个表中有个字符串类型的字段,值中有带字母的也有纯数字,如何找出纯数字的呢?今天发现了translate,当然还可以配合其它函数实现更大的功能!translate函数和replace函数的功能相似,区别是translate是以字符级进行替换操作,而replace函数则是以字符串级

当然也可以用正则regexp_like(id ,’^\d+$’)

实验开始

SQL> create table test1(id varchar2(5),
2  insert_date date);

Table created.

SQL> insert into test1 values(1,sysdate);

1 row created.

SQL> insert into test1 values(‘a’,sysdate);

1 row created.

SQL> insert into test1 values(‘a1’,sysdate);

1 row created.

SQL> insert into test1 values(‘1000′,sysdate);

1 row created.

SQL> insert into test1 values(’10b0’,sysdate);

1 row created.

SQL> insert into test1 values(‘1.5′,sysdate);

1 row created.

SQL> commit;

Commit complete.

SQL> select * from test1;

ID    INSERT_DATE
—– ——————-
1     2010-12-27 10:56:17
a     2010-12-27 10:56:33
a1    2010-12-27 10:56:37
1000  2010-12-27 10:56:43
10b0  2010-12-27 10:56:48
1.5   2010-12-27 10:57:24

SQL> select * from test1 where  trim(translate(id,’1234567890′,’          ‘)) is null
2  ;

ID    INSERT_DATE
—– ——————-
1     2010-12-27 10:56:17
1000  2010-12-27 10:56:43

SQL>

如果再加上大小限制可以这样,求id为数字且在1和10之间的

SELECT *
FROM (SELECT a.*, CAST (ID AS NUMBER) AS num
FROM test1 a
WHERE REGEXP_LIKE (ID, ‘^\d+$’))
WHERE num BETWEEN 1 AND 10

打赏

目前这篇文章有1条评论(Rss)评论关闭。

  1. Man Gosline | #1
    2011-12-21 at 06:16

    I love your wordpress format, where did you get a hold of it?