格式化函數提供一套有效的工具用于把各種數據類型(日期/時間,int,float,numeric)轉換成格式化的字符串以及反過來從格式化的字符串轉換成原始的數據類型。
表 5-6. 格式化函數
函數
返回
描述
例子
to_char(datetime, text)
text
把datetime 轉換成 string
to_char('now'::datetime, 'hh12:mi:ss')
to_char(timestamp, text)
text
把 timestamp 轉換成 string
to_char( now(), 'hh12:mi:ss')
to_char(int, text)
text
把 int4/int8 轉換成 string
to_char(125, '999')
to_char(float, text)
text
把 float4/float8 轉換成 string
to_char(125.8, '999d9')
to_char(numeric, text)
text
把 numeric 轉換成 string
to_char(-125.8, '999d99s')
to_datetime(text, text)
datetime
把 string 轉換成 datetime
to_datetime('05 dec 2000 13', 'dd mon yyyy hh')
to_date(text, text)
date
把 string 轉換成 date
to_date('05 dec 2000', 'dd mon yyyy')
to_timestamp(text, text)
date
把 string 轉換成 timestamp
to_timestamp('05 dec 2000', 'dd mon yyyy')
to_number(text, text)
numeric
把 string 轉換成 numeric
to_number('12,454.8-', '99g999d9s')
所有格式化函數都是 format-picture (格式圖)的第二個參數。
表 5-7. 用于日期/時間 to_char() 版本的format-pictures。
format-picture
描述
hh
一天中的小時 (01-12)
hh12
一天中的小時 (01-12)
mi
分鐘 (00-59)
ss
秒 (00-59)
ssss
過了午夜的秒 (0-86399)
y,yyy
帶逗號的年 (4 或更多位)
yyyy
年 (4 或更多位)
yyy
年的最后三位
yy
年的最后兩位
y
年的最后一位
month
完整的月份名(9字符)-所有字符大寫
month
完整的月份名(9字符)-首字符大寫
month
完整的月份名(9字符)-所有字符小寫
mon
縮寫的月份名(3字符)-所有字符大寫
mon
縮寫的月份名(3字符)-首字符大寫
mon
縮寫的月份名(3字符)-所有字符小寫
mm
月份(01-12)
day
完整的日期名(9字符)-所有字符大寫
day
完整的日期名(9字符)-首字符大寫
day
完整的日期名(9字符)-所有字符小寫
dy
縮寫的日期名(3字符)-所有字符大寫
dy
縮寫的日期名(3字符)-首字符大寫
dy
縮寫的日期名(3字符)-所有字符小寫
ddd
一年中的日子 (001-366)
dd
一月中的日子 (01-31)
d
一周中的日子 (1-7; sun=1)
w
月中的周
ww
年中的周
cc
世紀(兩位)
j
julian 日子(從4712 bc 年一月一日以來的日子)
q
季度
rm
羅馬數字的月份 (i-xii; i=jan)
所有 format-pictures (格式圖)允許使用后綴(后綴/前綴)。對于近似 format-picture(格式圖),后綴總是有效的。'fx'只是全局前綴。
表 5-8. 用于日期/時間 to_char() 版本的 format-pictures (格式圖)后綴。
后綴
描述
例子
fm
填充模式-前綴
fmmonth
th
大寫順序數-前綴
ddth
th
小寫順序數-后綴
ddth
fx
fx - (固定模式)全局 format-picture (格式圖)開關。如果沒有使用這個選項 to_datetime / to_date 忽略空白。必須作為formt-picture(格式圖)里的第一個項目使用。
fx month dd day
sp
拼寫模式(目前未實現)
ddsp
'/' - 必須用做雙 //,例如 '//hh//mi//ss'
'"' - 雙引號之間的字串被忽略并且不被分析。如果你想向輸出寫 '"' 你必須用 //",例如 '//"yyyy month//"'。
text - postgresql 的 to_char() 支持不帶 '"' 的文本,但是帶有雙引號的字串會快些并且可以保證該文本不會被解釋成關鍵字(format-picture,格式圖),例如 '"hello year: "yyyy'。
表 5-9. 用于數字 (int/float/numeric) to_char() 版本的format-pictures (格式圖)。
格式圖
描述
9
返回指定位數的值,如果不夠位數用空白代替
0
象 9 一樣,但是把空白替換成零
. (句點)
小數點
, (逗號)
分組(千進)分隔符
pr
在尖括號內返回負數
s
用負號返回負數(使用本地)
l
貨幣符號(使用本地)
d
小數點(使用本地)
g
分組符(使用本地)
mi
在指定位置返回負號(如果數字 < 0)
pl
在指定位置返回正號(如果數字 > 0) postgresql 擴展
sg
在指定位置返回正/負號(如果數字 < 0) - postgresql 擴展
rn
返回數字的羅馬數字(數字必須介于1 和 3999之間)
th 或th
把數字轉換成自然數(如果是負數或小數不轉換) - postgresql 擴展
v
arg1 * (10 ^ n);- 返回一個乘以 10^n (這里 'n' 是'v'后面的數字 '9')。to_char() 不支持同時使用 'v' 和小數點,如 "99.9v99"。
eeee
科學記數,目前不支持。
注意:通過a sign formatted via 'sg','pl' 或 'mi' 格式化的符號數不一定是數字;to_char(-12, 's9999')生成:
' -12'
,但是 to_char(-12, 'mi9999')生成:
'- 12'
。oracle 不允許在 '9' 前面使用 'mi',在 oracle 里,它總是在 '9' 后面。.
表 5-10. to_char()的例子
輸入
輸出
to_char(now(), 'day, hh12:mi:ss')
'tuesday , 05:39:18'
to_char(now(), 'fmday, hh12:mi:ss')
'tuesday, 05:39:18'
to_char( -0.1, '99.99')
' -.10'
to_char( -0.1, 'fm9.99')
'-.1'
to_char( 0.1, '0.9')
' 0.1'
to_char( 12, '9990999.9')
' 0012.0'
to_char( 12, 'fm9990999.9')
'0012'
to_char( 485, '999')
' 485'
to_char( -485, '999')
'-485'
to_char( 485, '9 9 9')
' 4 8 5'
to_char( 1485, '9,999')
' 1,485'
to_char( 1485, '9g999')
' 1 485'
to_char( 148.5, '999.999')
' 148.500'
to_char( 148.5, '999d999')
' 148,500'
to_char( 3148.5,'9g999d999')
' 3 148,500'
to_char( -485, '999s')
'485-'
to_char( -485, '999mi')
'485-'
to_char( 485, '999mi')
'485'
to_char( 485, 'pl999')
'+485'
to_char( 485, 'sg999')
'+485'
to_char( -485, 'sg999')
'-485'
to_char( -485, '9sg99')
'4-85'
to_char( -485, '999pr')
'<485>'
to_char( 485, 'l999')
'dm 485'
to_char( 485, 'rn')
' cdlxxxv'
to_char( 485, 'fmrn')
'cdlxxxv'
to_char( 5.2, 'fmrn')
'v'
to_char( 482, '999th')
' 482nd'
to_char( 485, '"good number:"999')
'good number: 485'
to_char( 485.8, '"pre-decimal:"999" post-decimal:" .999')
'pre-decimal: 485 post-decimal: .800'
to_char( 12, '99v999')
' 12000'
to_char( 12.4, '99v999')
' 12400'
to_char( 12.45, '99v9')
' 125'