2

我有一个使用 COPY 命令在 Redshift 中加载的数据源文件。

该文件有一堆具有两位数年份格式的日期列(我知道,我在这里处理恐龙)。

Redshift 可以识别日期格式,但问题是文件具有以下值:

06/01/79 

这实际上意味着:

2079-06-01

然而 Redshift 将其解释为:

1979-06-01

有没有办法告诉 Redshift 我对两位数日期格式的阈值是多少。例如,低于 90 的值应被解释为 20XX。

COPY 命令中的 DATEFORMAT 参数没有这样的选项。

4

1 回答 1

0
-- Begin transaction
BEGIN TRANS;
--  Create a temp table
CREATE TEMP TABLE my_temp (dtm_str CHAR(8));
-- Load your data into the temp table
COPY my_temp FROM s3://my_bucket … ;
-- Insert your data into the final table
INSERT INTO final_table
-- Grab the first 6 chars and concatenate to the following
SELECT CAST(LEFT(dtm_str,6)||
-- Convert the last 2 chars to and in and compare to your threshold
       CASE WHEN CAST(RIGHT(dtm_str,2) AS INT) < 85
-- Add either 1900 or 2000 to the INT, convert to CHAR
            THEN CAST(CAST(RIGHT(dtm_str,2) AS INT) + 2000 AS CHAR(4))
       ELSE CAST(CAST(RIGHT(dtm_str,2) AS INT) + 1900 AS CHAR(4)) END
-- Convert the final CHAR to a DATE
       AS DATE) new_dtm
FROM my_temp;
COMMIT;
于 2016-03-02T21:25:49.643 回答