gpt4 book ai didi

postgresql - 清理一些数据以导入 postgresql 的最佳方法?

转载 作者:行者123 更新时间:2023-11-29 14:37:42 35 4
gpt4 key购买 nike

我有两列,date 为 YYMMDD 格式,time 为 HHMMSS 格式,它们是类似 150103 132244 的字符串。这些接近十亿条记录中的四分之一。在导入 PostgreSQL 之前清理数据的最佳方法是什么?例如,有没有办法在导入时执行此操作?

最佳答案

您的数据可以使用函数 to_timestamp() 转换为带时区的时间戳:

with example(d, t) as (
values ('150103', '132244')
)

select d, t, to_timestamp(concat(d, t), 'yymmddhh24miss')
from example;

d | t | to_timestamp
--------+--------+------------------------
150103 | 132244 | 2015-01-03 13:22:44+01
(1 row)

您可以将文件导入到具有临时 列 (d, t) 的表中:

create table example(d text, t text);
copy example from ....

添加带有时区的时间戳列,转换数据并删除多余的文本列:

alter table example add tstamp_column timestamptz;

update example
set tstamp_column = to_timestamp(concat(d, t), 'yymmddhh24miss');

alter table example drop d, drop t;

关于postgresql - 清理一些数据以导入 postgresql 的最佳方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41984695/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com