Oracle中的格式化函数

格式化函数提供一套有效的工具用于把各种数据类型(日期/时间,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' 

 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/mantisXF/archive/2007/09/08/1777351.aspx

posted @ 2009-07-25 10:12  WenEric  阅读(421)  评论(0编辑  收藏  举报