Netezza INSERT - 忽略无效日期?

时间:2014-01-31 15:03:36

标签: sql netezza

是否有选项或设置会告诉Netezza忽略无效日期?在下面的示例中,整个INSERT失败,因为源表的第3行具有无效日期。我希望可以跳过违规行,但插入零行似乎有点极端。

我尝试了以下内容,但也失败了。另一种方法是解析源日期并验证每个组件的有效性(包括天/月,闰年等)。

insert into db.test ( cmclmn, effdt, efftm ) 
        select cmclmn, case when to_date(effdt,'yyyymmdd') is null 
                         then null 
                         else to_date(effdt,'yyyymmdd') end, 
               cast(lpad(efftm,6,0) as time) as efftm
          from db.test_src
          ;

整个脚本:

CREATE TABLE db.test
(
        cmclmn integer,
        effdt   date,
        efftm   time
        )
DISTRIBUTE ON ( cmclmn );

drop table db.test_src;
create table db.test_src
(
        cmclmn integer,
        effdt integer,
        efftm integer)
        distribute on ( cmclmn );

insert into db.test_src ( cmclmn, effdt, efftm ) values  ( 1,20140120, 102000); 
insert into db.test_src ( cmclmn, effdt, efftm ) values  ( 2,20140121, 231212); 
insert into db.test_src ( cmclmn, effdt, efftm ) values  ( 3,0,111111 ); 
insert into db.test_src ( cmclmn, effdt, efftm ) values  ( 4,20140123 ,90909 ); 

insert into db.test ( cmclmn, effdt, efftm ) 
        select cmclmn, to_date(effdt,'yyyymmdd'), cast(lpad(efftm,6,0) as time) as efftm
          from db.test_src
          ;

2 个答案:

答案 0 :(得分:0)

您可以创建用户定义的函数来执行完整测试。对于您的情况,也许一些更简单的事情会做:

insert into db.test ( cmclmn, effdt, efftm ) 
    select cmclmn, to_date(effdt,'yyyymmdd'), cast(lpad(efftm,6,0) as time) as efftm
    from db.test_src
    where effdt between 20010000 and 20150000 and
          effdt % 10000 between 0101 and 1231 and
          effdt between 01 and 31;

我不确定Netezza是否会优化查询,以便to_date()可能会在过滤器之前运行。如果是这样,那么也使用case

insert into db.test ( cmclmn, effdt, efftm ) 
    select cmclmn,
           (case when effdt between 20010000 and 20150000 and
                      effdt % 10000 between 0101 and 1231 and
                      effdt % 100 between 01 and 31
                 then to_date(effdt,'yyyymmdd')
            end),
           cast(lpad(efftm,6,0) as time) as efftm
    from db.test_src
    where effdt between 20010000 and 20150000 and
          effdt % 10000 between 0101 and 1231 and
          effdt % 100 between 01 and 31;

日期检查并不完美,但可能足以满足您的目的。

答案 1 :(得分:0)

Netezza Analytics Package 3.0附带了一些示例LUA函数,isdate()和todate()是其中的两个函数。

这些函数会在返回代码之前捕获返回代码。 isdate()返回一个布尔结果,告诉您日期是否有效,todate()将返回Netezza日期值,如果传入参数不是有效日期,则返回null。

感谢。