簡體   English   中英

使用SQL將日期dd / mm從txt導入表

[英]Import date dd/mm from txt to table with SQL

我是一個初學者,已經在各處搜索過,所以請多多包涵。

我有一個帶有此類數據(DD / MM)的txt文件; 作為分隔符:

01/10;10/06;15/11;10/07
01/10;10/06;15/11;10/07
01/11;20/06;10/11;30/07
01/11;20/06;10/11;30/07
10/11;20/06;20/01;30/07
01/10;01/06;15/11;30/06

首先,我set datestyle to European;

所以我有DateStyle-“ ISO,DMY”。

之后,我嘗試使用postgresql將此數據導入到pheno表的某些列中(請參見下面的代碼):

COPY pheno(planting_onset, harvesting_onset, planting_end, harvesting_end) 
FROM '/home/user/Documents/worldcrops/algeria_times.txt' DELIMITERS ';';

並給出了以下錯誤:

ERROR:  invalid input syntax for type date: "01/10"
CONTEXT:  COPY pheno, line 1, column planting_onset: "01/10"
********** Error **********

ERROR: invalid input syntax for type date: "01/10"
SQL state: 22007
Context: COPY pheno, line 1, column planting_onset: "01/10"

問題:如何將DD / MM數據類型復制到表中,列的日期為“數據類型”? 我應該更改“數據類型”列嗎?

提前致謝。

期望DMY,但是您只需要幾天和幾個月就可以了。 這有點hacky,但我認為應該可以:

ALTER TABLE pheno 
ADD planting_onset_temp VARCHAR(16), 
    harvesting_onset_temp VARCHAR(16),  
    planting_end_temp VARCHAR(16), 
    harvesting_end_temp VARCHAR(16);
COPY pheno(planting_onset_temp, harvesting_onset_temp, planting_end_temp, harvesting_end_temp) FROM '/home/user/Documents/worldcrops/algeria_times.txt' DELIMITERS ';';
UPDATE pheno 
SET planting_onset   = CONCAT(planting_onset_temp, '/2016'), 
    harvesting_onset = CONCAT(harvesting_onset_temp, '/2016'), 
    planting_end     = CONCAT(planting_end_temp, '/2016'),
    harvesting_end   = CONCAT(harvesting_end_temp, '/2016');
ALTER TABLE pheno DROP COLUMN planting_onset_temp, harvesting_onset_temp, planting_end_temp, harvesting_end_temp;

用相關年份替換“ / 2016”。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM