PostgreSQL data type formatting functions

Datatype formatting functions:

    PostgreSQL formatting functions provide a set of efficient tools for converting various datatypes (date/time, integer, floating point, and numeric) to formatted strings and back from formatted characters String to the specified data type. The functions are listed below, and they all follow a common calling convention: the first argument is the value to be formatted, and the second is a template that defines the output or output format.
function return type describe example
to_char(timestamp, text) text convert timestamp to string to_char(current_timestamp, ‘HH12:MI:SS’)
to_char(interval, text) text Convert time interval to string to_char(interval ‘15h 2m 12s’, ‘HH24:MI:SS’)
to_char(int, text) text convert integer to string to_char(125, ‘999’)
to_char(double precision, text) text Convert Real/Double to String 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_timestamp(text, text) timestamp convert string to timestamp to_timestamp(‘05 Dec 2000’, ‘DD Mon YYYY’)
to_timestamp(double) timestamp Convert UNIX epoch to timestamp to_timestamp(200120400)
to_number(text, text) numeric convert string to numeric to_number(‘12,454.8-‘, ‘99G999D9S’)

     1. Patterns for date/time formatting:

model describe
HH Hours of the day (01-12)
HH12 Hours of the day (01-12)
HH24 Hour of the day (00-23)
ME minutes (00-59)
SS Seconds (00-59)
MS milliseconds (000-999)
US Microseconds (000000-999999)
AM noon sign (uppercase)
Y,YYY year with comma (4 and more digits)
YYYY Years (4 and more digits)
YYY last three years
YY last two years
AND last of the year
MONTH Full-length month name (9 characters padded with blanks)
Month Full-length mixed-case month name (9 characters padded with blanks)
month Full-length lowercase month name (9 characters padded with blanks)
MON Uppercase abbreviated month name (3 characters)
Mon Abbreviated mixed case month name (3 characters)
mon Lowercase abbreviated month name (3 characters)
MM Month number (01-12)
DAY Date name in full length case (9 characters padded with blanks)
Day Full-length mixed-case date name (9 characters padded with blanks)
day Full-length lowercase date name (whitespace padded to 9 characters)
two Abbreviated uppercase date name (3 characters)
two Abbreviated mixed case date name (3 characters)
two Abbreviated lowercase date name (3 characters)
DDD Day of the year (001-366)
DD Day of the month (01-31)
D Day of the week (1-7; Sunday is 1)
W Week number (1-5) in a month (the first week starts on the first day of the month)
WW Week number in the year (1-53) (the first week starts on the first day of the year)

     2. Template mode for numeric formatting:

model describe
9 a value with the specified number of digits
0 value with leading zeros
. (Kukuten) decimal point
,(comma) grouping (thousands) separator
PR Negative values ​​in angle brackets
S signed value
L currency symbol
D decimal point
G grouping separator
ME minus sign at indicated position (if number < 0)
PL plus sign at indicated position (if number > 0)
SG plus/minus sign at the indicated position


8. Time/date functions and operators:

     1. The following is a list of time/date operators supported in PostgreSQL:

operator example result
+ date ‘2001-09-28’ + integer ‘7’ date ‘2001-10-05’
+ date ‘2001-09-28’ + interval ‘1 hour’ timestamp ‘2001-09-28 01:00’
+ date ‘2001-09-28’ + time ‘03:00’ timestamp ‘2001-09-28 03:00’
+ interval ‘1 day’ + interval ‘1 hour’ interval ‘1 day 01:00’
+ timestamp ‘2001-09-28 01:00’ + interval ‘23 hours’ timestamp ‘2001-09-29 00:00’
+ time ‘01:00’ + interval ‘3 hours’ time ‘04:00’
- - interval ‘23 hours’ interval ‘-23:00’
- date ‘2001-10-01’ - date ‘2001-09-28’ integer ‘3’
- date ‘2001-10-01’ - integer ‘7’ date ‘2001-09-24’
- date ‘2001-09-28’ - interval ‘1 hour’ timestamp ‘2001-09-27 23:00’
- time ‘05:00’ - time ‘03:00’ interval ‘02:00’
- time ‘05:00’ - interval ‘2 hours’ time ‘03:00’
- timestamp ‘2001-09-28 23:00’ - interval ‘23 hours’ timestamp ‘2001-09-28 00:00’
- interval ‘1 day’ - interval ‘1 hour’ interval ‘23:00’
- timestamp ‘2001-09-29 03:00’ - timestamp ‘2001-09-27 12:00’ interval ‘1 day 15:00’
* interval ‘1 hour’ * double precision ‘3.5’ interval ‘03:30’
/ interval ‘1 hour’ / double precision ‘1.5’ interval ‘00:40’

    2. 日期/时间函数:

函数 返回类型 描述 例子 结果
age(timestamp, timestamp) interval 减去参数,生成一个使用年、月的”符号化”的结果 age(‘2001-04-10’, timestamp ‘1957-06-13’) 43 years 9 mons 27 days
age(timestamp) interval 从current_date减去得到的数值 age(timestamp ‘1957-06-13’) 43 years 8 mons 3 days
current_date date 今天的日期    
current_time time 现在的时间    
current_timestamp timestamp 日期和时间    
date_part(text, timestamp) double 获取子域(等效于extract) date_part(‘hour’, timestamp ‘2001-02-16 20:38:40’) 20
date_part(text, interval) double 获取子域(等效于extract) date_part(‘month’, interval ‘2 years 3 months’) 3
date_trunc(text, timestamp) timestamp 截断成指定的精度 date_trunc(‘hour’, timestamp ‘2001-02-16 20:38:40’) 2001-02-16 20:00:00+00
extract(field from timestamp) double 获取子域 extract(hour from timestamp ‘2001-02-16 20:38:40’) 20
extract(field from interval) double 获取子域 extract(month from interval ‘2 years 3 months’) 3
localtime time 今日的时间    
localtimestamp timestamp 日期和时间    
now() timestamp 当前的日期和时间(等效于 current_timestamp)    
timeofday() text 当前日期和时间    

    3. EXTRACT,date_part函数支持的field:

描述 例子 结果
CENTURY 世纪 EXTRACT(CENTURY FROM TIMESTAMP ‘2000-12-16 12:21:13’); 20
DAY (月分)里的日期域(1-31) EXTRACT(DAY from TIMESTAMP ‘2001-02-16 20:38:40’); 16
DECADE 年份域除以10 EXTRACT(DECADE from TIMESTAMP ‘2001-02-16 20:38:40’); 200
DOW 每周的星期号(0-6;星期天是0) (仅用于timestamp) EXTRACT(DOW FROM TIMESTAMP ‘2001-02-16 20:38:40’); 5
DOY 一年的第几天(1 -365/366) (仅用于 timestamp) EXTRACT(DOY from TIMESTAMP ‘2001-02-16 20:38:40’); 47
HOUR 小时域(0-23) EXTRACT(HOUR from TIMESTAMP ‘2001-02-16 20:38:40’); 20
MICROSECONDS 秒域,包括小数部分,乘以 1,000,000。 EXTRACT(MICROSECONDS from TIME ‘17:12:28.5’); 28500000
MILLENNIUM 千年 EXTRACT(MILLENNIUM from TIMESTAMP ‘2001-02-16 20:38:40’); 3
MILLISECONDS 秒域,包括小数部分,乘以 1000。 EXTRACT(MILLISECONDS from TIME ‘17:12:28.5’); 28500
MINUTE 分钟域(0-59) EXTRACT(MINUTE from TIMESTAMP ‘2001-02-16 20:38:40’); 38
MONTH 对于timestamp数值,它是一年里的月份数(1-12);对于interval数值,它是月的数目,然后对12取模(0-11) EXTRACT(MONTH from TIMESTAMP ‘2001-02-16 20:38:40’); 2
QUARTER 该天所在的该年的季度(1-4)(仅用于 timestamp) EXTRACT(QUARTER from TIMESTAMP ‘2001-02-16 20:38:40’); 1
SECOND 秒域,包括小数部分(0-59[1]) EXTRACT(SECOND from TIMESTAMP ‘2001-02-16 20:38:40’); 40
WEEK 该天在所在的年份里是第几周。 EXTRACT(WEEK from TIMESTAMP ‘2001-02-16 20:38:40’); 7
YEAR 年份域 EXTRACT(YEAR from TIMESTAMP ‘2001-02-16 20:38:40’); 2001

    4. 当前日期/时间:
    我们可以使用下面的函数获取当前的日期和/或时间∶
    CURRENT_DATE
    CURRENT_TIME
    CURRENT_TIMESTAMP
    CURRENT_TIME (precision)
    CURRENT_TIMESTAMP (precision)
    LOCALTIME
    LOCALTIMESTAMP
    LOCALTIME (precision)
    LOCALTIMESTAMP (precision)

    该博客中提供的所有信息均源自PostgreSQL官方文档,编写该篇博客的主要目的是便于今后的查阅,特此声明。

转载自http://www.cnblogs.com/stephen-liu74/archive/2012/05/04/2294643.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324716554&siteId=291194637