什么是 PostgreSQL 相当于 Oracle 的 SYSDATE?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16960432/
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
What is PostgreSQL equivalent of SYSDATE from Oracle?
提问by user2365917
I want to perform a query using sysdate like:
我想使用 sysdate 执行查询,例如:
select up_time from exam where up_time like sysdate
which is possible in Oracle.
这在 Oracle 中是可能的。
However, it seems that PostgreSQL doesn't support sysdate. I couldn't find sysdate in postgres documentation. What is the replacement for sysdate in PostgreSQL?
但是,PostgreSQL 似乎不支持 sysdate。我在 postgres 文档中找不到 sysdate。PostgreSQL 中 sysdate 的替代品是什么?
回答by a_horse_with_no_name
SYSDATE
is an Oracle only function.
SYSDATE
是 Oracle 唯一的函数。
The ANSI standard defines current_date
or current_timestamp
which issupported by Postgres and documented in the manual:
http://www.postgresql.org/docs/current/static/functions-datetime.html#FUNCTIONS-DATETIME-CURRENT
ANSI标准定义current_date
或current_timestamp
其中是通过Postgres的支撑,并且记录在手册中:
http://www.postgresql.org/docs/current/static/functions-datetime.html#FUNCTIONS-DATETIME-CURRENT
(Btw: Oracle supports CURRENT_TIMESTAMP
as well)
(顺便说一句:Oracle 也支持CURRENT_TIMESTAMP
)
You should pay attention to the difference between current_timestamp
, statement_timestamp()
and clock_timestamp()
(which is explained in the manual, see the above link)
您应该注意current_timestamp
,statement_timestamp()
和之间的区别clock_timestamp()
(手册中有解释,请参阅上面的链接)
This statement:
这个说法:
select up_time from exam where up_time like sysdate
select up_time from exam where up_time like sysdate
Does not make anysense at all. Neither in Oracle nor in Postgres. If you want to get rows from "today", you need something like:
没有任何意义。无论是在 Oracle 中还是在 Postgres 中。如果你想从“今天”获取行,你需要这样的:
select up_time
from exam
where up_time = current_date
Note that in Oracle you would probably want trunc(up_time) = trunc(sysdate)
to get rid of the time part that is always included in Oracle.
请注意,在 Oracle 中,您可能希望trunc(up_time) = trunc(sysdate)
摆脱始终包含在 Oracle 中的时间部分。
回答by Virendra Patel
NOW()is the replacement of Oracle Sysdate in Postgres.
NOW()是 Postgres 中 Oracle Sysdate 的替代品。
Try "Select now()", it will give you the system timestamp.
尝试“ Select now()”,它会给你系统时间戳。
回答by segfault
You may want to use statement_timestamp(). This give the timestamp when the statement was executed. Whereas NOW()
and CURRENT_TIMESTAMP
give the timestamp when the transaction started.
您可能想要使用statement_timestamp()。这给出了执行语句时的时间戳。而NOW()
并CURRENT_TIMESTAMP
给出交易开始时的时间戳。
More details in the manual
手册中的更多详细信息
回答by abhinav kumar
The following functions are available to obtain the current date and/or time in PostgreSQL:
以下函数可用于获取 PostgreSQL 中的当前日期和/或时间:
CURRENT_TIME
CURRENT_DATE
CURRENT_TIMESTAMP
Example
例子
SELECT CURRENT_TIME;
08:05:18.864750+05:30
SELECT CURRENT_DATE;
2020-05-14
SELECT CURRENT_TIMESTAMP;
2020-05-14 08:04:51.290498+05:30