从 PostgreSQL 中的 NOW() 结果中删除 TIMESTAMP 精度?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12060546/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Remove TIMESTAMP precision from NOW() result in PostgreSQL?
提问by littleK
Is there any way to remove the precision from the result of the NOW() function in PostgreSQL?
有没有办法从 PostgreSQL 中 NOW() 函数的结果中删除精度?
"2012-08-21 10:23:34.867502"
I'm looking for the format to be:
我正在寻找的格式是:
"2012-08-21 10:23:34"
I'm attempting to update a column of type "timestamp without time zone" with the following SQL:
我正在尝试使用以下 SQL 更新“没有时区的时间戳”类型的列:
UPDATE table SET column = now();
Thanks!
谢谢!
回答by Scott Marlowe
Simple answer is to cast it to zero precision.
简单的答案是将其转换为零精度。
select now()::timestamptz(0);
回答by pilcrow
回答by Allenile
You can change the structure of your table il you move the length of the column to 0 in pgAdmin 3 or if you create a table by using timestamp(0)like that :
您可以更改表的结构,如果您在 pgAdmin 3 中将列的长度移动到 0,或者如果您使用这样的时间戳(0)创建表:
CREATE TABLE public.users
(
id integer serial,
username character varying(255) NOT NULL,
email character varying(255) NOT NULL,
password character varying(255) NOT NULL,
created_at timestamp(0) without time zone NOT NULL,
updated_at timestamp(0) without time zone NOT NULL,
CONSTRAINT users_pkey PRIMARY KEY (id)
);
But if you do that an error will occur if you try to insert a timestamp with millisecond.
但是如果你这样做,如果你尝试插入一个毫秒的时间戳,就会发生错误。
回答by Rasjid Wilcox
Depending on your requirements, another option is to adjust the precision of the timestamp column itself - setting its precision to 0. This can be useful when using PostgreSQL with legacy programs that don't handle fractional seconds in timestamps.
根据您的要求,另一种选择是调整时间戳列本身的精度 - 将其精度设置为 0。这在将 PostgreSQL 与不处理时间戳中的小数秒的遗留程序一起使用时非常有用。