to_char

TO_CHAR()函数有三种情况,分别是:

1、

    TO_CHAR ( nchar | clob | nclob )

    以接受NCHAR, NVARCHAR2, CLOB, NCLOB类型的字符串作为自己的参数,把它们转换成数据库的字符集的字符串。请看下面这个例子:

这个是national 字符集的情况:


这个是clob的情况:

09:59:03 SQL> create table t(a clob) ;

Table created.


09:59:15 SQL> insert into t values ('aaaaaaaaaaaaaaaaaaaaaaaaaa') ;

1 row created.


09:59:31 SQL> select * from t;

A

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

aaaaaaaaaaaaaaaaaaaaaaaaaaa


10:00:03 SQL> select to_char(a) from t

TO_CHAR(A)

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

aaaaaaaaaaaaaaaaaaaaaaaaaaa




2、

    TO_CHAR ( date [, fmt [, 'nlsparam'] ])

       它的作用是将一个日期date转换成一个指定格式fmt的字符串。有关nlsparam基本可以不管,它是用来确定返回的日期的名字以及缩写词等的形式,如你的数据库语言如果是英语,那么返回的月份表示可能是类似这样的“MAY”;而如果你的数据库语言是汉语,那么返回的月份表示可能是类似这样的“五月”。nlsparam就是用来改变这些显示,如假设你的数据库语言是汉语,而你像把它变成英语的表示,则可以这样设置:nls_date_language=’english’。99%可以不用管这个参数。如下面这个例子:

10:13:29 SQL> select to_char(sysdate,'yyyy-mm-dd') today from dual;

TODAY

----------

2007-09-24
下面请看一些例子:

10:13:29 SQL> select to_char(sysdate,'yyyy-mm-dd') today from dual;

TODAY

----------

2007-09-24


13:27:59 SQL> select to_char(sysdate,'yyyy-mm-dd,d') today from dual;

TODAY

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

2007-09-24,2


13:28:23 SQL> select to_char(sysdate,'yyyy-mm-dd,day') today from dual;

TODAY

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

2007-09-24,monday


13:29:25 SQL> select to_char(sysdate,'yyyy-mm-dd,dy') today from dual;

TODAY

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

2007-09-24,mon


13:30:49 SQL> select to_char(sysdate+1,'yyyy-mm-dd,dy') today from dual;

TODAY

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

2007-09-25,tue


13:34:58 SQL> select to_char(systimestamp,'yyyy-mm-dd,hh24:mi:ss.ff3') now from dual;

NOW

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

2007-09-24,13:35:18.107


13:35:18 SQL> select to_char(systimestamp,'yyyy-mm-dd,hh24:mi:ss.ff2') now from dual;

NOW

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

2007-09-24,13:35:29.51


13:35:29 SQL> select to_char(systimestamp,'yyyy-mm-dd,hh24:mi:ss.ff9') now from dual;

NOW

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

2007-09-24,13:35:59.301998000


13:35:59 SQL> select to_char(sysdate,'yyyy-mon-dd') today from dual;

TODAY

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

2007-sep-24


13:46:27 SQL> select to_char(sysdate,'yyyy-month-dd') today from dual;

TODAY

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

2007-september-24


13:49:18 SQL> select to_char(hiredate,'yyyy-month-dd') d from emp;

D

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

1980-december -17

1981-february -20

1981-february -22

1981-april    -02

1981-september-28

1981-may      -01

1981-june     -09

1987-april    -19

1981-november -17

1981-september-08

1987-may      -23

1981-december -03

1981-december -03

1982-january -23

14 rows selected.


13:49:33 SQL> select to_char(sysdate,'yyyy-month-dd,ssss') now from dual;

NOW

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

2007-september-24,3939


13:53:39 SQL> select to_char(sysdate,'yyyy-month-dd,hh24:mi:ss.ssss') now from dual;

NOW

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

2007-september-24,14:03:25.2525


14:03:25 SQL> select to_char(sysdate,'yyyy-month-dd,hh24:mi:ss,ssss') now from dual;

NOW

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

2007-september-24,14:03:40,4040


15:23:34 SQL> create table t ( a date);

Table created.


15:23:42 SQL> insert into t values (to_date('-1023-10-5','syyyy-mm-dd'));

1 row created.


15:25:02 SQL> select to_char(a,'syyyy-mm-dd') d from t;

D

-----------

-1023-10-05


15:25:10 SQL> select to_char(a,'yyyy-mm-dd,ad') d from t;

D

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

1023-10-05,bc


15:25:25 SQL> select to_char(a,'syear-mm-dd') d from t;

D

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

-ten twenty-three-10-05


15:30:35 SQL> select to_char(sysdate,'syear-mm-dd') d from dual;

D

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

two thousand seven-09-24


3、

    TO_CHAR ( n [,fmt [, 'nlsparam'] ])


       含义几乎和TO_CHAR_DATE类似,无非参数nlsparam表示用于显示数字的表示而已,它也几乎不使用。这个你们有关数字的fmt,和TO_CHAR_DATE作用类似用于控制数字的显示,但是它几乎不会使用,因此也在此省略。

16:15:31 SQL> select to_char(1234567890) d from dual;

D

----------

1234567890


16:15:55 SQL> select to_char(1234567890,'099999999999999') d from dual;

D

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

000001234567890


16:16:12 SQL> select to_char(1234567890,'999,999,999,999,999') d from dual;

D

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

1,234,567,890


16:22:23 SQL> select to_char(1234567890,'99.999,999,999') d from dual;

select to_char(1234567890,'99.999,999,999') d from dual                         

*

ERROR at line 1:

ORA-01481: invalid number format model


16:22:46 SQL> select to_char(1234567890,'99.999') d from dual;

D

-------

#######


16:22:57 SQL> select to_char(1234567890,'999,999,999,999,999.99999999') d from dual;

D

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

你可能感兴趣的:(sql,D语言)