如何在postgresql中使用copy命令加载时转换日期列?

时间:2017-05-24 08:19:02

标签: sql postgresql shell amazon-web-services amazon-redshift

使用copy命令在PostgreSQL中加载日期列时遇到问题。 实际上,列值,日期

207     2017-03-08T01:25:34.000Z
207     2017-03-23T09:33:03.000Z
1266    2017-03-08T01:25:35.000Z
1327    2017-03-08T01:25:35.000Z

我无法使用时间戳读取2017-03-08 01:25:34等日期格式。

所以我使用varchar类型来读取日期列。

现在我想创建一个新列作为日期格式和阅读日期,如2017-03-08 01:25:34。

我的预期输出是

207     2017-03-08T01:25:34.000Z    2017-03-08 01:25:34
207     2017-03-23T09:33:03.000Z    2017-03-23 09:33:03
1266    2017-03-08T01:25:35.000Z    2017-03-08 01:25:35
1327    2017-03-08T01:25:35.000Z    2017-03-08 01:25:35

如何在PostgreSQL中使用copy命令加载时执行此操作。

3 个答案:

答案 0 :(得分:0)

您可以查看日期修饰符here

我确信这适用于您,但您仍可在上面的链接中查看更多选项:  to_timestamp(text, text)例如:to_timestamp('05 Dec 2000', 'DD MM YYYY HH12:MI:SS')

答案 1 :(得分:0)

使用COPY加载DATA时无法执行此操作。但是使用现有方法,您可以从现有varchar更新新的时间戳列。 例如:

const NestedRoutes = () => (
    <div>
        <h2>This is my next nest</h2>
        <Switch>
            <Route exact path='/nextnest' component={Nest}/>
            <Route path='/nextnest/about' component={NestAbout}/>
            <Route component={NoMatch}/>
        </Switch>
    </div>
)

const SecondLevelNesting = () => (
  <Router>
      <MainLayout>
          <Switch>
              <Route exact path="/" component={Home}/>
              <Route path="/about" component={About}/>
              <Route path="/nextnest" component={NestedRoutes}
              <Route component={NoMatch}/>
          </Switch>
      </MainLayout>
    </div>
  </Router>
);
postgres 8.0翻译

更新应该这样做:

t=# select regexp_replace('2017-03-08T01:25:35.000Z','[TZ]',' ','g')::timestamp;
   regexp_replace
---------------------
 2017-03-08 01:25:35
(1 row)

答案 2 :(得分:0)

我不明白为什么它不起作用:

测试数据:

$ cat data.csv
207,2017-03-08T01:25:34.000Z
207,2017-03-23T09:33:03.000Z
1266,2017-03-08T01:25:35.000Z
1327,2017-03-08T01:25:35.000Z

创建测试表:

$ psql -h server -U postgres -c "create table ts_test(id int,ts timestamp with time zone)" db
CREATE TABLE

\copy数据:

$ psql -h server -U postgres -c "\copy ts_test(id,ts) FROM 'data.csv' DELIMITER ',' CSV" db
COPY 4

查询结果:

$ psql -h server -U postgres -c "select * from ts_test" db
  id  |           ts
------+------------------------
  207 | 2017-03-08 03:25:34+02
  207 | 2017-03-23 11:33:03+02
 1266 | 2017-03-08 03:25:35+02
 1327 | 2017-03-08 03:25:35+02
(4 rows)

如果您也希望以字符串格式使用它,请首先创建表格:

$ psql -h server -U postgres -c "create table ts_test(id int,ts timestamp with time zone, str character varying(100))" db
CREATE TABLE

然后\copy数据和UPDATE str列(注意:WHERE可能需要一些UPDATE,因此UPDATE被注释掉了):

$ psql -h server -U postgres -c "\copy ts_test(id,ts) FROM 'data.csv' DELIMITER ',' CSV; --update ts_test set str=ts::character varying(100);" db
UPDATE 4

$ psql -h server -U postgres -c "select * from ts_test;" db
  id  |           ts           |          str
------+------------------------+------------------------
  207 | 2017-03-08 03:25:34+02 | 2017-03-08 03:25:34+02
  207 | 2017-03-23 11:33:03+02 | 2017-03-23 11:33:03+02
 1266 | 2017-03-08 03:25:35+02 | 2017-03-08 03:25:35+02
 1327 | 2017-03-08 03:25:35+02 | 2017-03-08 03:25:35+02