如果一个表中有个字符串类型的字段,值中有带字母的也有纯数字,如何找出纯数字的呢?今天发现了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
当然也可以用正则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
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




