Oracle的to char函式使用方法

2021-08-08 03:42:09 字數 4400 閱讀 3840

postgres

格式化函式提供一套有效的工具用於把各種資料型別(日期/時間,int,float,numeric)轉換成格式化的字串以及反過來從格式化的字串轉換成原始的資料型別。

注意:所有格式化函式的第二個引數是用於轉換的模板。
表 5-7. 格式化函式 函式

返回描述

例子to_char(timestamp, text)

text

把 timestamp 轉換成 string

to_char(timestamp '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(numeric '-125.8', '999d99s')

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')

表 5-8. 用於 date/time 轉換的模板 模板

描述hh

一天的小時數 (01-12)

hh12

一天的小時數 (01-12)

hh24

一天的小時數 (00-23)

mi分鐘 (00-59)

ss秒 (00-59)

ssss

午夜後的秒 (0-86399)

am or a.m. or pm or p.m.

正午標識(大寫)

am or a.m. or pm or p.m.

正午標識(小寫)

y,yyy

帶逗號的年(4 和更多位)

yyyy

年(4和更多位)

yyy年的後三位

yy年的後兩位

y年的最後一位

bc or b.c. or ad or a.d.

年標識(大寫)

bc or b.c. or ad or a.d.

年標識(小寫)

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世紀(2 位)

jjulian 日期(自西元前2023年1月1日來的日期)q季度

rm羅馬數字的月份(i-xii;i=jan)-大寫

rm羅馬數字的月份(i-xii;i=jan)-小寫

所有模板都都允許使用字首和字尾修改器。模板裡總是允許使用修改器。字首 'fx' 只是乙個全域性修改器。

表 5-9. 用於日期/時間模板 to_char() 的字尾 字尾

描述例子

fm填充模式字首

fmmonth

th大寫順序數字尾

ddth

th小寫順序數字尾

ddth

fx固定模式全域性選項(見下面)

fx month dd day

sp拼寫模式(還未實現)

ddsp

用法須知:

表 5-10. 用於 to_char(numeric

) 的模板 模板

描述9帶有指定位數的值

0前導零的值

. (句點)

小數點, (逗號)

分組(千)分隔符

pr尖括號內負值

s帶負號的負值(使用本地化)

l貨幣符號(使用本地化)

d小數點(使用本地化)

g分組分隔符(使用本地化)

mi在指明的位置的負號(如果數字 < 0)

pl在指明的位置的正號(如果數字 > 0)

sg在指明的位置的正/負號

rn羅馬數字(輸入在 1 和 3999 之間)

th or th

轉換成序數

v移動 n

位(小數)(參閱註解)

eeee

科學記數。現在不支援。

用法須知:

表 5-11. 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')

' cdl***v'

to_char(485,'fmrn')

'cdl***v'

to_char(5.2,'fmrn')

vto_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'

Oracle 函式專題 TO CHAR

to char函式提供一套有效的工具用於把各種資料型別 日期 時間,int,float,numeric 轉換成格式化的字串以及反過來從格式化的字串轉換成原始的資料型別 模板一select to char sysdate,模板 from dual 模板如下 hh 一天的小時數 01 12 hh12 一...

to char函式oracle怎麼用

檢視字元占用位元組 select userenv language dump 我 length 我 字元數 lengthb 我 位元組數 vsize 我 from dual to char函式使用 9 代表乙個數字 0 強迫0顯示 顯示美元符號 l 強制顯示乙個當地的貨幣符號 顯示乙個小數點 顯示乙...

Oracle中to Char的用法

最近用oracle比較多,今天遇到了to char這個函式,簡單總結一下to char的用法,希望對大家有所幫助。一 下圖是to char函式的幾個例子 二 下圖是to char函式用於將日期轉換為字串時的有效引數列表,這些引數可以在許多情況下使用 三 下圖是to char函式的日期示例 四 在一些...