gpt4 book ai didi

postgresql - 默认时间戳格式和小数秒

转载 作者:行者123 更新时间:2023-11-29 11:40:40 27 4
gpt4 key购买 nike

我正在尝试将我的 Postgres 数据库中的时间戳格式化为特定格式:

YYYY-MM-DD HH24:MI:SS

通过做:

update myTable set tds = to_char(tds, 'YYYY-MM-DD HH24:MI:SS')::timestamp;

我设法将所有以前存储的 tds 设置为这种格式。但是,任何新添加的条目都会返回到:YYYY-MM-DD HH24:MI:SS.MS,因为默认设置为 now()

如何更改此设置,使新添加的条目也具有以下格式:YYYY-MM-DD HH24:MI:SS

最佳答案

时间戳类型没有存储格式。您可以将其默认设置为在创建时截断为秒的时间戳

create table t (
tds timestamp default date_trunc('second', now())
)

或者修改表格

alter table t 
alter column tds
set default date_trunc('second', now());

insert into t values (default);
INSERT 0 1

select * from t;
tds
---------------------
2014-03-11 19:24:11

如果你只是不想显示毫秒部分格式化输出

select to_char(now(), 'YYYY-MM-DD HH24:MI:SS');
to_char
---------------------
2014-03-11 19:39:40

关于postgresql - 默认时间戳格式和小数秒,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22332425/

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