[20260522]关于时间格式ss与sssss的说明.txt
--//今天在测试使用dbms_profiler遇到的问题,作者使用日期格式如下:
dbms_output.put_line('Start: '||to_char(sysdate,'hh24:mi:sssss'));
--//参考链接:http://fritshoogland.wordpress.com/2012/11/26/profiling-plsql-with-dbms_profiler/
--//出现5个s,看文档才发现显示为Seconds past midnight (0-86399).也就是一天的秒数。才想起前段时间写的秒转换时分秒的脚本
--//s2hms.sql.
--//顺便测试还可以发现一些有趣的细节:
1.环境:
SCOTT@book01p> @ ver2
==============================
PORT_STRING : x86_64/Linux 2.4.xx
VERSION : 21.0.0.0.0
BANNER : Oracle Database 21c Enterprise Edition Release 21.0.0.0.0 - Production
BANNER_FULL : Oracle Database 21c Enterprise Edition Release 21.0.0.0.0 - Production
Version 21.3.0.0.0
BANNER_LEGACY : Oracle Database 21c Enterprise Edition Release 21.0.0.0.0 - Production
CON_ID : 0
PL/SQL procedure successfully completed.
$ cat s2hms.sql
column "hh24:mi:ss" format a10
select to_char(to_date(&&1,'sssss'),'hh24:mi:ss') "hh24:mi:ss" from dual;
2.测试:
SCOTT@book01p> select to_char(sysdate,'hh24:mi:ss') c30 , to_char(sysdate,'hh24:mi:sssss') c20,sysdate from dual ;
C30 C20 SYSDATE
------------------------------ -------------------- -------------------
17:20:20 17:20:62420 2026-05-22 17:20:20
SCOTT@book01p> @ s2hms 62420
hh24:mi:ss
----------
17:20:20
--//实际上如果少写一个s。
SCOTT@book01p> select to_char(sysdate,'hh24:mi:ss') c30 , to_char(sysdate,'hh24:mi:ssss') c20,sysdate from dual ;
C30 C20 SYSDATE
------------------------------ -------------------- -------------------
17:22:46 17:22:4646 2026-05-22 17:22:46
--//这里ssss表示2个秒数,也就是46秒。
--//如果输入是7个s。
SCOTT@book01p> select to_char(sysdate,'hh24:mi:ss') c30 , to_char(sysdate,'hh24:mi:sssssss') c20,sysdate from dual ;
C30 C20 SYSDATE
------------------------------ -------------------- -------------------
17:24:20 17:24:6266020 2026-05-22 17:24:20
--//优先选择5S,形成5s+2s的组合。
--//表示一天的秒数+秒数。
SCOTT@book01p> @ s2hms 62660
hh24:mi:ss
----------
17:24:20
--//你可以选择2,5个s的多个组合,其他情况会报错。例子:
SCOTT@book01p> select to_char(sysdate,'hh24:mi:ss') c30 , to_char(sysdate,'hh24:mi:ssssssssss') c20,sysdate from dual ;
C30 C20 SYSDATE
------------------------------ -------------------- -------------------
17:29:19 17:29:6295962959 2026-05-22 17:29:19
--//10个s,显示2次1天的秒数。
SCOTT@book01p> select to_char(sysdate,'hh24:mi:ss') c30 , to_char(sysdate,'hh24:mi:sssssssss') c20,sysdate from dual ;
C30 C20 SYSDATE
------------------------------ -------------------- -------------------
17:30:45 17:30:630454545 2026-05-22 17:30:45
--//根据优先选择5s的组合方式,选择5s+2s+2s的组合方式.
--//9个s,显示1天的秒数+秒数+秒数。
SCOTT@book01p> select to_char(sysdate,'hh24:mi:ss') c30 , to_char(sysdate,'hh24:mi:sssssssssss') c20,sysdate from dual ;
select to_char(sysdate,'hh24:mi:ss') c30 , to_char(sysdate,'hh24:mi:sssssssssss') c20,sysdate from dual
*
ERROR at line 1:
ORA-01821: date format not recognized
--//根据优先选择5s的组合方式,11个组合为5+5+1,没有单个s的格式报错。