postgres格式化时间_PostgreSQL格式化与日期函数

PostgreSQL格式化函数提供一套有效的 工具 用于把各种数据类型(日期/时间、integer、floating point和numeric)转换成格式化的字符串以及反过来从格式化的字符串转换成指定的数据类型。下面列出了这些函数,它们都遵循一个公共的调用习惯:第一个参数是待格式化的值,而第二个是定义输出或输出格式的模板。

函数

返回类型

描述

例子

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

把numeric转换成字串

to_char(-125.8, ‘999D99S’)

to_date(text, text)

date

把字串转换成日期

to_date(‘05 Dec 2000’, ‘DD Mon YYYY’)

to_timestamp(text, text)

timestamp

把字串转换成时间戳

to_timestamp(‘05 Dec 2000’, ‘DD Mon YYYY’)

to_timestamp(double)

timestamp

把UNIX纪元转换成时间戳

to_timestamp(200120400)

to_number(text, text)

numeric

把字串转换成numeric

to_number(‘12,454.8-‘, ‘99G999D9S’)

1. 用于日期/时间格式化的模式:

模式

描述

HH

一天的小时数(01-12)

HH12

一天的小时数(01-12)

HH24

一天的小时数(00-23)

MI

分钟(00-59)

SS

秒(00-59)

MS

毫秒(000-999)

US

微秒(000000-999999)

AM

正午标识(大写)

Y,YYY

带逗号的年(4和更多位)

YYYY

年(4和更多位)

YYY

年的后三位

YY

你可能感兴趣的:(postgres格式化时间)