Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2833874
  • 博文数量: 599
  • 博客积分: 16398
  • 博客等级: 上将
  • 技术积分: 6875
  • 用 户 组: 普通用户
  • 注册时间: 2009-11-30 12:04
个人简介

WINDOWS下的程序员出身,偶尔也写一些linux平台下小程序, 后转行数据库行业,专注于ORACLE和DB2的运维和优化。 同时也是ios移动开发者。欢迎志同道合的朋友一起研究技术。 数据库技术交流群:58308065,23618606

文章分类

全部博文(599)

文章存档

2014年(12)

2013年(56)

2012年(199)

2011年(105)

2010年(128)

2009年(99)

分类: Oracle

2010-09-06 11:24:09

   很多时候我们的日期可能存放的是字符串类型,在插入的时候也很有可能插入的日期格式不正确,
因此我们需要找出这些不合符的日期格式,来此来修正。当然可以使用TO_DATE函数一个一个的转换来找出不合法的日期。ORACLE提供了正则表达式,正则表达式在处理不合符的IP ,手机号,EMAIL等大有用处。
此处我们用正则表达式找出不合法的日期格式。


SQL> conn ysp/ysp
Connected.
SQL> select * from v$version;

BANNER
----------------------------------------------------------------------------------------------------
Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - 64bi
PL/SQL Release 10.2.0.4.0 - Production
CORE    10.2.0.4.0      Production
TNS for IBM/AIX RISC System/6000: Version 10.2.0.4.0 - Productio
NLSRTL Version 10.2.0.4.0 - Production

SQL> create table  regexp_test(datecol char(19));

Table created.

SQL> insert into regexp_test select LAST_DDL_TIME  from user_objects where rownum<=10;

10 rows created.

SQL> commit;

Commit complete.

SQL> select * from regexp_test;

DATECOL
--------------------------------------
2010-07-06 16:56:07
2010-06-12 16:16:36
2010-06-12 13:44:14
2010-06-12 14:23:33
2010-06-12 14:48:21
2010-07-26 10:44:27
2010-06-04 10:20:03
2010-06-04 13:22:31
2010-06-04 10:26:31
2010-06-04 13:50:02

10 rows selected.

此处我们用YYYY-MM-DD HH24:MI:SS作为我们的日期格式,其他格式为非法。

SQL> update regexp_test set datecol='2010-00-06 16:56:07' where datecol='2010-07-06 16:56:07';

1 row updated.

SQL> update regexp_test set datecol='2010-06-00 16:16:36' where datecol='2010-06-12 16:16:36';

1 row updated.

SQL> update regexp_test set datecol='2010-06-12 24:44:14' where datecol='2010-06-12 13:44:14';

1 row updated.

SQL> update regexp_test set datecol='2010-06-12 14:60:33' where datecol='2010-06-12 14:23:33';

1 row updated.

SQL> update regexp_test set datecol='2010-06-12 14:48:67' where datecol='2010-06-12 14:48:21';

1 row updated.

SQL> commit;

Commit complete.

SQL> insert into regexp_test values('2010-06-04:13:50:02');

1 row created.

SQL> commit;

Commit complete.

SQL> select * from regexp_test;

DATECOL
---------------------------
2010-00-06 16:56:07  --月份不合法
2010-06-00 16:16:36  --日子不合法
2010-06-12 24:44:14  --小时不合法
2010-06-12 14:60:33  --分钟不合法
2010-06-12 14:48:67  --秒数不合法
2010-07-26 10:44:27
2010-06-04 10:20:03
2010-06-04 13:22:31
2010-06-04 10:26:31
2010-06-04 13:50:02
2010-06-04:13:50:02  --格式不合法

11 rows selected.

 

找出合法的日期

SQL> select * from regexp_test
  2  where regexp_like(datecol,'^([0-9]{4})-((0[1-9])|(1[0-2]))-((0[1-9])|([1-2][0-9])|(3[0-1])) (([0-1][0-9])|(2[0-3])):([0-5][0-9]):([0-5][0-9])$');

DATECOL
--------------------------------------
2010-07-26 10:44:27
2010-06-04 10:20:03
2010-06-04 13:22:31
2010-06-04 10:26:31
2010-06-04 13:50:02

找出不合法的日期


SQL> select * from regexp_test
  2  where not regexp_like(datecol,'^([0-9]{4})-((0[1-9])|(1[0-2]))-((0[1-9])|([1-2][0-9])|(3[0-1])) (([0-1][0-9])|(2[0-3])):([0-5][0-9]):([0-5][0-9])$');

DATECOL
--------------------------------------
2010-00-06 16:56:07
2010-06-00 16:16:36
2010-06-12 24:44:14
2010-06-12 14:60:33
2010-06-12 14:48:67
2010-06-04:13:50:02

6 rows selected.

 

阅读(4100) | 评论(2) | 转发(0) |
给主人留下些什么吧!~~

四月04042010-09-07 18:45:21

您好!非常喜欢你的文章,非常期待认识你,可以加一下MSn吗?我的是:zqh0404@hotmail.com

chinaunix网友2010-09-07 11:19:50

Download More than 1000 free IT eBooks: http://free-ebooks.appspot.com