9.8. 型別轉換函式
PostgreSQL 格式化函數提供了一套功能強大的工具,用於將各種資料型 別(日期/時間、整數、浮點數、數字)轉換為格式化的字串,以及從格式化字串轉換為特定資料型別。
Table 9.24
列出了這些函數,而這些函數都遵循一個通用的呼叫約定:第一個參數是要格式化的值,第二個參數是定義輸出或輸入格式的樣板。
|
Function
|
Return Type
|
Description
|
Example
|
|---|---|---|---|
to_char(timestamp
,
text
)
|
text
|
將時間戳記轉換為字串
|
to_char(current_timestamp, 'HH12:MI:SS')
|
to_char(interval
,
text
)
|
text
|
convert 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 precision 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_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')
|
小技巧