Avicii2018

转载: https://blog.csdn.net/dtjiawenwang88/article/details/73295181

 

在实际的工作中会经常会用到to_char()、to_date()函数来对时间、日期进行处理。

1、to_char()函数的用法

 1.1、将时间日期按照指定的格式输出,得到的是字符串,而非date类型。

select 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;
运行的输出结果为:
2017/6/15 17:07:24 2017-06-15
2017/6/15 17:07:25 2017/06/15
2017/6/15 17:07:25 20170615
2017/6/15 17:07:25 20170615 17:07:25
 1.2、用to_char()可以得到日期中的年、月、日、时、分
select 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;
运行的输出结果为:
2017/6/15 17:09:14 2017
2017/6/15 17:09:14 06
2017/6/15 17:09:14 17
2017/6/15 17:09:14 09
注:to_char()得到的是字符串,要查询具体单日、时、分要特别注意。 
select accept_time,to_char(accept_time,\'mi\') from TMP_WW_0615_GYTS_S2
where to_char(accept_time,\'mi\')=\'06\' ;
select accept_time,to_char(accept_time,\'mi\') from TMP_WW_0615_GYTS_S2
where to_char(accept_time,\'mi\')=\'6\' ;
 运行输出结果为:
2017/6/8 21:06:59 06
null
2、to_date()函数的用法

 2.1、将字符串转换为具体指定的时间日期格式

select sysdate,to_date(\'20170615\',\'yyyymmdd\')from dual;
select sysdate,to_date(\'20170615\',\'yyyy-mm-dd\')from dual;
select sysdate,to_date(\'20170615\',\'yyyy/mm/dd\')from dual;
select sysdate,to_date(\'20170615\',\'yyyy-mm-dd hh24:mi:ss\')from dual;
 运行输出结果为:
2017/6/15 17:20:27 2017/6/15
2017/6/15 17:20:27 2017/6/15
2017/6/15 17:20:27 2017/6/15
2017/6/15 17:20:27 2017/6/15
注:to_date()得到的日期格式是和系统的日期格式保持一致;

      得到的时间为当天的 00 :00:00。
2.2、可以直接使用date\'yyyy-mm-dd\'
select date\'2017-5-1\',to_date(\'20170615\',\'yyyymmdd\')from dual;
 运行输出结果为:
2017/5/1 2017/6/15
注:date\'2017/5/1\' 会提示格式不对。

 

转载: https://blog.csdn.net/dtjiawenwang88/article/details/73295181

分类:

技术点:

相关文章: