[英]netezza compare char date in YYYY-MM-DD format to parameter date in yyyy-mm-dd format
我正在查詢一個日期為 YYYY-MM-DD 格式但格式為字符的文件。 如果我只是運行一個簡單的查詢,比如 WHERE DATE_IN_FILE >= '2022-01-31' 就可以了。 但是我需要將該日期作為參數,因此不必對其進行硬編碼。 我已經創建了這樣的參數: select trim(to_char(to_date(trim(date_today),'yyyymmdd')+1,'yyyy-mm-dd')) into v_eff_dt from model_input_param date_today is formatted as character in yyyymmdd format , 前任。 20220630。所以我想將 date_in_file 與 v_eff_dt 進行比較。 我嘗試了多種方法來做到這一點,但似乎沒有任何效果。 這是我嘗試過的一些方法:
希望能得到一些幫助。 謝謝
假設您在(外部)文件中有這些數據
cat /tmp/dat
2022-01-02
2022-02-02
2022-04-02
2023-03-12
2023-05-22
2023-06-03
現在在 Netezza 中,您可以即時閱讀這些內容(或創建一個外部表然后閱讀)
select * from external'/tmp/dat' (date_in_file date) ;
DATE_IN_FILE
--------------
2022-01-02
2022-02-02
2022-04-02
2023-03-12
2023-05-22
2023-06-03
(6 rows)
現在您可以將參數存儲在表中,然后像這樣過濾
-- or insert
create table v_eff_dt as
select duration_add(current_date, 1::numeric(8,0));
INSERT 0 1
with tempfile(date_in_file) as (
select * from external'/tmp/dat' (date_in_file date)
) select *
from tempfile, v_eff_dt
where tempfile.date_in_file <= dt;
DATE_IN_FILE | DT
--------------+------------
2022-01-02 | 2022-07-09
2022-02-02 | 2022-07-09
2022-04-02 | 2022-07-09
(3 rows)
但這取決於v_eff_dt
總是有 1 行有效。 這樣做要好得多
select *
from external'/tmp/dat' (date_in_file date)
where date_in_file <= duration_add(current_date, 1::numeric(8,0))
DATE_IN_FILE
--------------
2022-01-02
2022-02-02
2022-04-02
(3 rows)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.