【问题标题】:Redshift COPY statement loading date format with two digit year (mm/dd/yy)Redshift COPY 语句加载日期格式,带两位数年份 (mm/dd/yy)
【发布时间】:2016-06-08 12:06:53
【问题描述】:

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

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

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

06/01/79 

这实际上意味着:

2079-06-01

然而 Redshift 将其解释为:

1979-06-01

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

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

【问题讨论】:

  • 作为替代方案,如果您不想/不能事先编辑文件,您可以在COPY 之后使用UPDATE,在其中使用DATEADD(year,100,timestamp) 修复年份。如有必要,利用临时表。
  • 我认为更新临时表是最好的解决方案。文件太大,无法在代码中处理。

标签: postgresql date amazon-redshift y2k


【解决方案1】:
-- 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;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-02-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-25
    • 1970-01-01
    • 2012-03-05
    相关资源
    最近更新 更多