一、to_char() 与 to_date()函数
to_char()
将时间日期按照指定的格式输出,得到的是字符串,而非date类型。1
2
3
4select sysdate,to_char(sysdate, 'yyyy-mm-dd') from dual;
select sysdate,to_char(sysdate, 'yyyy/mm/dd') from dual;
select sysdate,to_char(sysdate, 'yyyymmdd') from dual;
select sysdate,to_char(sysdate, 'yyyymmdd hh24:mi:ss') from dual;也可以用to_char()得到单独的年月日时分秒的字符串
1
2
3
4select sysdate,to_char(sysdate,'yyyy') from dual;
select sysdate,to_char(sysdate,'mm') from dual;
select sysdate,to_char(sysdate,'hh24') from dual;
select sysdate,to_char(sysdate,'mi') from dual;to_date()
将字符串转换为具体指定的时间日期格式1
2
3
4select sysdate,to_date('20190103','yyyymmdd') from dual;
select sysdate,to_date('20190103','yyyy-mm-dd') from dual;
select sysdate,to_date('20190103','yyyy/mm/dd') from dual;
select sysdate,to_date('20190103','yyyy-mm-dd hh24:mi:ss') from dual;
二、时间戳转换为时间
1 | SELECT TO_CHAR(时间戳的那一列 / (1000 * 60 * 60 * 24) + |
三、获取当前时间戳
1 | select (sysdate - to_date('1970-01-01 08:00:00','yyyy-mm-dd hh24:mi:ss'))*86400000 from dual; |