参考官方文档:
https://www.postgresql.org/docs/10/functions-formatting.html
https://www.postgresql.org/docs/10/functions-datetime.html
查询当前时间 Current Date/Time
Postgresql提供了许多函数,它们返回与当前日期和时间相关的值。这些sql标准函数都基于当前事务的开始时间返回值:
时间相关值
select CURRENT_DATE
select CURRENT_TIME
select CURRENT_TIMESTAMP
select CURRENT_TIME(precision )
select CURRENT_TIMESTAMP(precision )
select LOCALTIME
select LOCALTIMESTAMP
select LOCALTIME(precision )
select LOCALTIMESTAMP(precision )
CURRENT_TIME
和CURRENT_TIMESTAMP
是带时区的值;LOCALTIME
和LOCALTIMESTAMP
是不带时区的值。precision
是时间精度,传整型值,1代表毫秒保留1位,2代表毫秒保留2位
时间相关函数
select transaction_timestamp()
select statement_timestamp()
select clock_timestamp()
select timeofday()
select Now()
transaction_timestamp()
等效于CURRENT_TIMESTAMP
statement_timestamp()
返回当前语句的开始时间(更具体地说,是从客户端收到最新命令消息的时间)。statement_timestamp()
和transaction_timestamp()
在事务的第一个命令期间返回相同的值,但在后续命令期间可能有所不同。clock_timestamp()
返回实际的当前时间,在单个sql命令中,其值也会更改。timeofday()
是Postgresql的历史功能。像clock_timestamp()
一样,它返回实际的当前时间,但以格式化的文本字符串的形式返回,而不是带有时区值的时间戳。Now()
等效于transaction_timestamp()
。
时间类型转换的方法
Postgresql格式化功能提供了将各种数据类型(日期/时间,整数,浮点数,数字)转换为格式化字符串,以及从格式化字符串转换为特定数据类型。这些函数都遵循一个通用的调用约定:第一个参数是要格式化的值,第二个参数是定义输出或输入格式的模板。
Function | Return Type | Description | Example |
---|---|---|---|
to_char(timestamp, text) | text | 将时间戳转换为字符串 | to_char(current_timestamp,‘HH12:MI:SS’) |
to_char(interval, text) | text | 将间隔转换为字符串 | to_char(interval ‘15h 2m 12s’, ‘HH24:MI:SS’) |
to_char(int, text) | text | 将整型数转换为字符串 | to_char(125, ‘999’) |
to_char(double precision, text) | text | 将实数/双精度转换为字符串 | to_char(125.8::real, ‘999D9’) |
to_char(numeric, text) | text | convert numeric to string | to_char(-125.8, ‘999D99S’) |
to_date(text, text) | date | convert string to date | to_date(‘05 Dec 2000’, ‘DD Mon YYYY’) |
to_number(text, text) | numeric | convert string to numeric | to_number(‘12,454.8-’, ‘99G999D9S’) |
to_timestamp(text, text) | timestamp with time zone | convert string to time stamp | to_timestamp(‘05 Dec 2000’, ‘DD Mon YYYY’) |
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。