Oracle 中 TO_CHAR用法
(1)用作日期转换:
to_char(date,'格式');
select to_date('2005-01-01 ','yyyy-MM-dd') from dual; select to_char(sysdate,'yyyy-MM-dd HH24:mi:ss') from dual;
(2)处理数字:
to_char(number,'格式');
select to_char(88877) from dual; select to_char(1234567890,'099999999999999') from dual; select to_char(12345678,'999,999,999,999') from dual; select to_char(123456,'99.999') from dual; select to_char(1234567890,'999,999,999,999.9999') from dual;
(3)to_char(salary,'$99,99');
select TO_CHAR(123,'$99,999.9') from dual;
(4)用于进制转换:将10进制转换为16进制;
select to_char(4567,'xxxx') from dual; select to_char(123,'xxx') from dual;
//// to_char 例子
①其9代表:如果存在数字则显示数字,不存在则显示空格
②其0代表:如果存在数字则显示数字,不存在则显示0,即占位符。
③其FM代表:删除如果是因9带来的空格,则删除之
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'
Oralce 中TO_CHAR( 日期型值 , 格式参数) 用法
Oracle 中 TO_CHAR 函数用法如下列表
SYSDATE | 2009-6-16 15:25:10 | |
TRUNC(SYSDATE) | 2009-6-16 | |
TO_CHAR(SYSDATE,'YYYYMMDD') | 20090616 | 到日 |
TO_CHAR(SYSDATE,'YYYYMMDD HH24:MI:SS') | 20090616 15:25:10 | 到秒 |
TO_CHAR(SYSTIMESTAMP,'YYYYMMDD HH24:MI:SS.FF3') | 20090616 15:25:10.848 | 到毫秒 |
TO_CHAR(SYSDATE,'AD') | 公元 | |
TO_CHAR(SYSDATE,'AM') | 下午 | |
TO_CHAR(SYSDATE,'BC') | 公元 | |
TO_CHAR(SYSDATE,'CC') | 21 | |
TO_CHAR(SYSDATE,'D') | 3 | 老外的星期几 |
TO_CHAR(SYSDATE,'DAY') | 星期二 | 星期几 |
TO_CHAR(SYSDATE,'DD') | 16 | |
TO_CHAR(SYSDATE,'DDD') | 167 | |
TO_CHAR(SYSDATE,'DL') | 2009年6月16日 星期二 | |
TO_CHAR(SYSDATE,'DS') | 2009-06-16 | |
TO_CHAR(SYSDATE,'DY') | 星期二 | |
TO_CHAR(SYSTIMESTAMP,'SS.FF3') | 10.848 | 毫秒 |
TO_CHAR(SYSDATE,'FM') | ||
TO_CHAR(SYSDATE,'FX') | ||
TO_CHAR(SYSDATE,'HH') | 03 | |
TO_CHAR(SYSDATE,'HH24') | 15 | |
TO_CHAR(SYSDATE,'IW') | 25 | 第几周 |
TO_CHAR(SYSDATE,'IYY') | 009 | |
TO_CHAR(SYSDATE,'IY') | 09 | |
TO_CHAR(SYSDATE,'J') | 2454999 | |
TO_CHAR(SYSDATE,'MI') | 25 | |
TO_CHAR(SYSDATE,'MM') | 06 | |
TO_CHAR(SYSDATE,'MON') | 6月 | |
TO_CHAR(SYSDATE,'MONTH') | 6月 | |
TO_CHAR(SYSTIMESTAMP,'PM') | 下午 | |
TO_CHAR(SYSDATE,'Q') | 2 | 第几季度 |
TO_CHAR(SYSDATE,'RM') | VI | |
TO_CHAR(SYSDATE,'RR') | 09 | |
TO_CHAR(SYSDATE,'RRRR') | 2009 | |
TO_CHAR(SYSDATE,'SS') | 10 | |
TO_CHAR(SYSDATE,'SSSSS') | 55510 | |
TO_CHAR(SYSDATE,'TS') | 下午 3:25:10 | |
TO_CHAR(SYSDATE,'WW') | 24 | |
TO_CHAR(SYSTIMESTAMP,'W') | 3 | |
TO_CHAR(SYSDATE,'YEAR') | TWO THOUSAND NINE | |
TO_CHAR(SYSDATE,'YYYY') | 2009 | |
TO_CHAR(SYSTIMESTAMP,'YYY') | 009 | |
TO_CHAR(SYSTIMESTAMP,'YY') | 09 |
未例:
select trunc(sysdate,'IW') from dual; 本周第一天
select trunc(sysdate,'yy') from dual ; 本年第一天
select trunc(sysdate,'mm') from dual; 本月第一天
select trunc(sysdate,'day')+1 from dual; 本周第一天
select to_char(SYSTIMESTAMP,'dd-mon-yy HH12:MI:SS.FF4 AM') from dual;
select TO_char(TO_TIMESTAMP('30-11月-10 02:20:00.7490 下午','dd-mon-yy HH12:MI:SS.FF4 AM'),'YYYY/MM/DD HH24:MI:SS') from DUAL;