2014-01-31 46 views
1

有沒有可以告訴Netezza忽略無效日期的選項或設置?在下面的例子中整個INSERT失敗,因爲源表的第三行有一個無效的日期。我期望違規行可能會被跳過,但插入零行似乎是一個極端的事情。Netezza INSERT - 忽略無效日期?

我嘗試了以下,但它也失敗了。另一種方法是解析源日期和檢驗的有效性每個組件(包括天/月,閏年等)

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,2014,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 
      ; 

回答

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; 

日期檢查是不完美的,但它可能足以滿足您的目的。

0

Netezza Analytics Package 3.0附帶一些示例LUA函數,isdate()和todate()是其中的兩個函數。

這些函數在查詢返回代碼之前捕獲返回代碼。 isdate()返回布爾結果,告訴您日期是否有效,todate()將返回Netezza日期值,如果傳入參數不是有效日期,則返回null。

謝謝。