TO_CHAR:转换成字符串
TO_NUMBER:转换成数字
TO_DATE:转换成日期
example:SELECT empno,ename,TO_CHAR(hiredate,'yyyy')year,
TO_CHAR(hiredate,'mm')months,
TO_CHAR(hiredate,'dd')day from emp;
EMPNO ENAME YEAR MO DA
--------- ---------- ---- -- --
7369 SMITH 1980 12 17
7499 ALLEN 1981 02 20
7521 WARD 1981 02 22
7566 JONES 1981 04 02
7654 MARTIN 1981 09 28
7698 BLAKE 1981 05 01
7782 CLARK 1981 06 09
7788 SCOTT 1987 04 19
7839 KING 1981 11 17
7844 TURNER 1981 09 08
7876 ADAMS 1987 05 23
EMPNO ENAME YEAR MO DA
--------- ---------- ---- -- --
7900 JAMES 1981 12 03
7902 FORD 1981 12 03
7934 MILLER 1982 01 23
可以使用to_char()函数进行日期显示的转换功能。
SQL> select empno,ename,TO_CHAR(hiredate,'yyyy-mm-dd')from emp;
EMPNO ENAME TO_CHAR(HI
---------- ---------- ----------
7369 SMITH 1980-12-17
7499 ALLEN 1981-02-20
7521 WARD 1981-02-22
7566 JONES 1981-04-02
7654 MARTIN 1981-09-28
7698 BLAKE 1981-05-01
7782 CLARK 1981-06-09
7788 SCOTT 1987-04-19
7839 KING 1981-11-17
7844 TURNER 1981-09-08
7876 ADAMS 1987-05-23
7900 JAMES 1981-12-03
7902 FORD 1981-12-03
7934 MILLER 1982-01-23
如果是5月,则会使用05表示,那么这个0称为前导0.如果不希望显示前导0的话,则可以使用fm去掉这些0
SQL> select empno,ename,TO_CHAR(hiredate,'fmyyyy-mm-dd')hiredate from emp;
EMPNO ENAME HIREDATE
---------- ---------- ----------
7369 SMITH 1980-12-17
7499 ALLEN 1981-2-20
7521 WARD 1981-2-22
7566 JONES 1981-4-2
7654 MARTIN 1981-9-28
7698 BLAKE 1981-5-1
7782 CLARK 1981-6-9
7788 SCOTT 1987-4-19
7839 KING 1981-11-17
7844 TURNER 1981-9-8
7876 ADAMS 1987-5-23
7900 JAMES 1981-12-3
7902 FORD 1981-12-3
7934 MILLER 1982-1-23
TO_CHAR()函数也可以用在数字上。
在数字中加入一些符号以分割太长的数字,9表示一位数字
SQL> select empno,ename,TO_CHAR(sal,'99,999')sal from emp;
EMPNO ENAME SAL
---------- ---------- -------
7369 SMITH 800
7499 ALLEN 1,600
7521 WARD 1,250
7566 JONES 2,975
7654 MARTIN 1,250
7698 BLAKE 2,850
7782 CLARK 2,450
7788 SCOTT 3,000
7839 KING 5,000
7844 TURNER 1,500
7876 ADAMS 1,100
7900 JAMES 950
7902 FORD 3,000
7934 MILLER 1,300
在数字前面加入货币符号可以使用$,L(local,表示本地)
SQL> select empno,ename,TO_CHAR(sal,'$99,999')sal from emp;
EMPNO ENAME SAL
---------- ---------- --------
7369 SMITH $800
7499 ALLEN $1,600
7521 WARD $1,250
7566 JONES $2,975
7654 MARTIN $1,250
7698 BLAKE $2,850
7782 CLARK $2,450
7788 SCOTT $3,000
7839 KING $5,000
7844 TURNER $1,500
7876 ADAMS $1,100
7900 JAMES $950
7902 FORD $3,000
7934 MILLER $1,300
SQL> select empno,ename,TO_CHAR(sal,'L99,999')sal from emp;
EMPNO ENAME SAL
---------- ---------- -----------------
7369 SMITH ¥800
7499 ALLEN ¥1,600
To_NUMBER()将字符串转换为数字的函数
TO_DATE()函数可以将一个字符串转换为DATE类型
SQL> select TO_DATE('2009-02-16','yyyy-mm-dd') FROM DUAL;
TO_DATE('2009-
--------------
16-2月 -09