oracle function TO_CHAR (x [[, c2], C3])

[Function] or the date data into char data type
Parameters
x is a number or a date data type.
c2 format parameter
set to the parameter c3 NLS
if x is nlsparm = NLS_DATE_LANGUAGE control language date month and day of the return parts used.
If x is a number used to specify nlsparm = NLS_NUMERIC_CHARACTERS thousandth decimal places and separator, and currency symbols.
NLS_NUMERIC_CHARACTERS = "dg", NLS_CURRENCY = "string"

[] returns varchar2 Character


[1] described type when the data x

c1 Format Reference Table:

No.

format

Jane cases

Explanation

1

,(comma)

'9999,999'

Comma, generally thousandths appear as a grouping symbol to use. If you need to you can also be used as a very, hundredths appear, can appear N times, depending on the size of the numbers.

Examples of metamorphosis is to_char (1234, '9,9,9,9').

Note: You can only appear in the integer part.

2

. (Dot)

'99.99'

No point, do not read the "full stop", a period is a circle, a good point can occur only where the corresponding decimal point can only occur once.

to_char(1234.34,'9,9,9,9.99')

Note: You can only appear in one place, the original data is decimal point position

3

$ (Dollar sign)

'$999.99'

Dollars. In fact, you can be placed anywhere (at 10G)

to_char(1234.34,'9,9,9,9.$99')

Note: You can only occur once.

4

0 (zero)

'0999.99'

Zero. Returns the corresponding character at a corresponding position, if not places '0' padding.

to_char (0.34, '9,9,9,0. $ 99') = '$ 0.34'; to_char (1234, '9999.00') = '1234.00';

Note: This is a mandatory sign, there is no corresponding bit, places 'o' filling, which is very different places 9

5

9

'999.99'

9. In decimal, then converted into a corresponding character, if no places represents 0; bit integer, no corresponding character is not filled.

to_char (123, '999.99') = 123.00; TO_CHAR (123, '99999.9') = 123.0;

Note: For 0 and 9, the format is not as if the median number of digits and more, returns the '#'.

For example to_char (12345, '9999') = '#####'

6

B (blank)

'B999'

No other special action, the whole part of top a space may occur in any position.

'S'||TO_CHAR(1234,'99B99')='S 1234';

Note: You can only appear in the integer part.

7

C (international currency symbol)

'C9999'

It returns an ISO currency symbol at a specific location (the value is represented by the parameter NLS_ISO_CURRENCY)

TO_CHAR (1233, 'C9999') = 'CNY1234', this is the new international standard RMB, can be found on the "international currency symbol."

Note: You can only appear in the integer part first.

Can alter session set NLS_ISO_CURRENCY = 'JAPAN'; set to modify the current session.

8

D (ISO decimal notation)

'999D99'

This is the "dot" international version (ISO), and is equivalent to the dot, also appear only once. The difference is, the contents of the database will be set according to the parameter value NLS_NUMERIC_CHARACTER The default value it is the point of this number.

Note: There is no particular need generally do not use this format symbols do not easily change parameter values.

Alter sesssion set can also be used to modify.

alter session set nls_numeric_characters='!,'; to_char(1234.34,'9999d99')=1234!34

9

EEEE (Scientific Computing symbol)

9.9EEEE

Scientific Computing symbol

TO_CHAR (2008032001, '9.9EEEE') = '2.01E + 09', as is the computational scientific approach, so the front plus a decimal or 0 to 9, a plurality of no sense.

10

G (grouping symbols)

999G999

A comma (,) of the ISO standard, as a packet symbols, can be placed in multiple locations.

TO_CHAR(123456,'999G9G99')=123,4,56

Note: with the eighth -D, in addition if you want to convert a decimal point, and D will have to be used in conjunction, can not and dot with.

11

L (local currency symbol)

'L999'

Is the local version of C can be placed in the front and rearmost entire format.

TO_CHAR(123456,'999G9G99D00L')=123,4,56.00¥

Note: with the seventh C

12

MI (minus)

'9999MI'

If it is negative, at the end of a negative sign (-), if it is positive, then the tail put spaces

to_char(1234,'9999mi')||'S'||TO_CHAR(-5678,'9999MI') =1234 S5678-

Note: You can only format on the tail

13

PR (symbol)

9999PR

It is another way of expressing negative if it is positive, plus the head space; if it is negative, then with a small simple brackets <> digital wrap.

TO_CHAR(-1234.89,'9G999D00PR')=<1,234.89>

注意事项:同12

14

RN(rn)

RN(rn)

把整数(1-3999)转换为罗马字符.RN表示转为大写,rn表示小写的.

declare

i int;

begin

for i in 1..20 loop

dbms_output.put_line(to_char(i,'RN'));

end loop;

end;

注意事项:只能自己使用,不能和其它符号组合使用.

15

S

'9999S'

是12,13的综合改进版本.为整数加一个正号+,为负数加一个符号-.S在前则加在前,在后则在后.

TO_CHAR(-1234,'S9999')=-1234;TO_CHAR(1234,'S9999')=+1234

16

TM

TM9/TMe

使用这个参数等于没有用参数to_char(number)一样,应为'tm9'是默认的格式参数.

to_char(1234,'tme')=1234

注意事项:格式要么是TM9,要么是TME.

当数字长度超过64位时候,TM9的输出等同于TME的输出.

17

U

U999

双币符号,例如欧元.作用同11的L

TO_CHAR(999,'U999')=¥999

注意事项:通过NLS_DUAL_CURRENCY 控制

18

V

999V9

这是个比较古怪,又不是很常使用的符号。它的作用在于做一个计算。

例如TO_CHAR(N,'999V9'),以p表示V的位置,则该表达式=to_char(N×(10的P-1次方)).但是9个数又必须保证大于等于乘积之后表示的位数.

TO_CHAR(5,'9V')=5*1=5;

TO_CHAR(5,'9V9')=5*10=50

TO_CHAR(5,'9V99')=500

TO_CHAR(50,'9V99')='######' 9的个数不够

注意事项:格式中不能和小数表达写在一起,但是可以混合货币等。

19

X

xxxx

转换为16进制。

TO_CHAR(100,'XX')= 64

注意事项:数值必须是大于等于0的整数。前面只能和0或者FM组合使用.

20

   

通过以上的例子,我们了解了各种数字的格式。可以说格式太多样,难于记在脑子,最好是作为一个参考存在着.

归类:

数值类: 0,9,

分组类: (.),(,),D,G ,其中点好和逗号因为表示不明显,所以用小括号凸显。

货币类: $,C,L,U

计算转换类:EEEE,RN,V,X

正负符号:MI,PR,S

其它类:B

正统类:TM

【示例】
to_char(1210.73, '9999.9') 返回 '1210.7' 
to_char(1210.73, '9,999.99') 返回 '1,210.73' 
to_char(1210.73, '$9,999.00') 返回 '$1,210.73' 
to_char(21, '000099') 返回 '000021' 
to_char(852,'xxxx') 返回' 354'


【说明2】x为日期型,c2可用参数
 

序号

格式

简例

说明

1

- / , . ; :

时间分隔符号,除了标准的几个,还允许用文字作为分割符号。

"text"

例如 to_char(sysdate,'YYYY"年"mm"月"dd"日"')=2008年04月24日

2

AD

 

即拉丁文Anno Domini的简写,表示公元.会根据nls的不同转换为公元或者ad等

A.D.

无特殊注意事项

3

AM

 

上午的简写 ,同pm, p.m. (下午) , 中文环境输出为上午(如果是上午)

A.M.

4

BC

 

虽然标准的写法是B.c. (c小写) 或者BC,好在Oracle不讲究这个。表示公元前

B.C.

5

CC

 

返回世纪,以阿拉伯数字表示

SCC

如果年的后两位介于01-99那么,返回前两位+1,否则返回前两位

6

D

 

一周之中的某天,返回的是序号1-7

7

DAY

 

一周之中的某天,不过返回的是星期几而已,这和语言设置有关系,在中国环境 NLS_DATE_LANGUAGE=SIMPLIFIED CHINESE ,用星期一到星期天表示

8

DD

 

月份中的某天(1-31)

9

DDD

 

年份中的某天(1-366)

10

DL

'DL'

返回长的日期格式。受到NLS_TERRITORY,NLS_LANGUAGE参数控制。例 2008年4月28日 星期一

限制:除了DL,其它什么的都不能设置。

11

DS

 

返回短的日期格式。受到NLS_TERRITORY,NLS_LANGUAGE参数控制。 例如 2008-04-28

限制:除了DL,其它什么的都不能设置。

12

DY

 

日期的简称,就是星期几(当然这指的是中国环境下)

13

E

 

纪元简称,但是只适合以下集中日历:日本皇室,中华民国,太过佛历

14

EE

 

纪元全程,适合情况同E

15

FF [1..9]

 

就是毫秒,如果不更上数字就是用默认的精度。

只能用于timestamp类型的。

16

FM

 

值得注意的一个函数:不返回任何内容。

有点不明白oracle为什么设置这个东西.

17

FX

 

同上

18

HH

 

表示小时,为12小时制,同hh12(1-12)

19

HH12

 

表示小时,为12小时制(1-12)

20

HH24

 

表示小时,为24小时制(0-23)

21

IW

 

ISO标准的星期序号(1-52,或者1-53)

22

IYYY

 

IYY,IY,I, ISO年(4位)的4,3,2,1位数字(倒数)

IYY

to_char(to_date(21120401,'yyyymmdd'),'iyyy, iyy,iy,i')=2112, 112,12,2

IY

 

I

 

23

J

 

儒略日(多用于天文的一种日历),从公元前4712年一月一日算起,得出的结果是个整数,算法大体为 (公元日期+4712)*儒略日历年平均天数

24

MI

 

秒(0-59)

25

MM

 

2位月(1-12)

26

MON

 

月的简称,和国家有关系NLS_DATE_LANGUAGE,例如04在中文环境下用4月表示.

27

MONTH

 

月的名称,国家有关系NLS_DATE_LANGUAGE,目前在中文下04表示为4月。

28

PM

 

同am,a.m.表示下午

P.M.

29

Q

 

季度(1-4)

30

RM

 

用罗马数字表示的月份,I ,II ,III ,IV ,V ,VI ,VII ,VIII,IX ,X ,XI ,XII

31

RR

 

有点四舍五入表示年的意思,具体的用法有那么一点点复杂。

以s表示输入的年份最后两位,c表示当前的年份最后两位,其输出结果(新的年份前两位)可以用函数r=f(s,c)来表示,s2,c2分别表示s,c的前两位。

1)s=[0,49],c=[0,49],则r=c2

2) s=[0,49],c=[50,99],则 r=c2+1

3) s=[50,99],c=[0,49],则r=c2-1

4) s=[50,99],c=[50,99],则 r=c2

简而言之就是靠近当前年份原则,如果和当前年份同区域那么就一样,如果比当前区域大,那么就是当作是当前世纪前一世纪,否则就是下一个世纪。

举例来说,以to_date为例子

SQL> select to_date('89-01-01','rr-mm-dd') ,to_date('12-01-01','rr-mm-dd') FROM DUAL;

 

TO_DATE('89-01-01','RR-MM-DD') TO_DATE('12-01-01','RR-MM-DD')

------------------------------ ------------------------------

1989-01-01 2012-01-01

我想oracle会搞这个东东出来,估计有两个考虑一个是为了方便,一个是为了对付百年或者千年问题。

32

RRRR

 

如果输入参数只有两位,则同rr,否则就同yyyy作用.

33

SS

 

秒(0-59),一分钟内

34

SSSSS

 

一天从午夜开始的累积秒数.(0-86399)

35

TS

 

返回短日期格式内容,包括时分秒等,只能和dl,ds组合使用,格式是:

dl ts或者dl ts ,中间以空格间隔开。TO_CHAR(SYSDATE,'TS')=下午 4:50:04

表现形式受NLS_TERRITORY 和NLS_LANGUAGE影响。

36

TZD

 

夏令时制信息,时区简写加上夏令时信息,必须和格式tzr设置的时区对应。

包括下面三个TZ开头的,都是和时区相关,并不是直接用在to_char

37

TZH

 

时区中的小时,例如hh:mi:ss.fftzh:tzm'

38

TZM

 

时区中的分钟.

39

TZR

 

时区中的区域信息,必须是数据库支持的时区,例如US/Pacific

40

WW

 

和iw类似,也是表示星期的序号,从年的第一天算起到年的最后一个第七天。二者取值基本相同。(1-53) ,例如2008-01-01 到2008-01-07 算1,2008-01-09~2008-01-13 算2

41

W

 

一个月中的星期序号,其算法同ww,不过是局限在一月之内而已,和iso的不同。

42

X

 

代表本地根符号,没有特别用处,只能和timestamp类型一起使用.

43

Y,YYY

 

四位年,用都好分隔 例如2,008

44

YEAR

 

发音表达的年,例如 2008=two thousand eight

SYEAR

S前缀表示公元前BC

45

YYYY

 

四位年,S前缀表示公元前BC

SYYYY

46

YYY

 

一次表示后面3,2,1位的年,例如2008 可以分别取值为008,08,8

YY

Y

 

总结

 

从以上看,主要就是表示时间几个部分的格式:世纪、年,月,日,时,分,秒,毫秒,以及其它一些混合格式。每个时间部分都可以有多种的表达方式,通过这样归类就比较容易记忆。

很多格式可以组合使用,这样最终可以形成足够丰富的表达其形势;

其次很多格式和nls是密切相关的;最后某些输出(返回)和格式大小写是有关系的,这在中文环境下体现不出来(目前来没有看到),但是english环境下就名下,以to_char(sysdate,'day')为例子,如果是西文环境是返回sun(假设sysdate位于周末),如果to_char(sysdate,'DAY')则返回SUN

【示例】
to_char(sysdate,'d') 每周第几天 
to_char(sysdate,'dd') 每月第几天 
to_char(sysdate,'ddd') 每年第几天 
to_char(sysdate,'ww') 每年第几周 
to_char(sysdate,'mm') 每年第几月 
to_char(sysdate,'q') 每年第几季 
to_char(sysdate,'yyyy') 年


SQL> select to_char(sysdate,' PM yyyy-mm-dd hh24:mi:sssss AD year mon day ddd iw') FROM DUAL;
TO_CHAR(SYSDATE,'PMYYYY-MM-DDH
--------------------------------------------------------------------------------
上午 2008-03-27 09:58:35917 公元 two thousand eight 3月 星期四 087 13
SQL> SELECT TO_CHAR(SYSTIMESTAMP,'HH24:MI:SS.FF5') FROM DUAL;
TO_CHAR(SYSTIMESTAMP,'HH24:MI:
------------------------------
10:02:28.90000
SQL>SELECT TO_CHAR(SYSDATE,'DS DL') FROM DUAL
TO_CHAR(SYSDATE,'DSDL')
-----------------------------------
2008-03-27 2008年3月27日 星期四

【示例】带C3示例

select to_char(to_date('2002-08-26','yyyy-mm-dd'),'day','NLS_DATE_LANGUAGE = American') from dual; 
返回:monday

Guess you like

Origin www.cnblogs.com/fanweisheng/p/11119693.html
C2