Oracle函数

原文:https://blog.csdn.net/dtjiawenwang88/article/details/73295181

1、trunc() 函数:
转换成指定日期格式 (对于日期)
--Oracle trunc()函数的用法
/**************日期********************/
1.select trunc(sysdate) from dual --2011-3-18 今天的日期为2011-3-18
2.select trunc(sysdate, 'mm') from dual --2011-3-1 返回当月第一天.
3.select trunc(sysdate,'yy') from dual --2011-1-1 返回当年第一天
4.select trunc(sysdate,'dd') from dual --2011-3-18 返回当前年月日
5.select trunc(sysdate,'yyyy') from dual --2011-1-1 返回当年第一天
6.select trunc(sysdate,'d') from dual --2011-3-13 (星期天)返回当前星期的第一天
7.select trunc(sysdate, 'hh') from dual --2011-3-18 14:00:00 当前时间为14:41
8.select trunc(sysdate, 'mi') from dual --2011-3-18 14:41:00 TRUNC()函数没有秒的精确
/***************数字********************/
/*
TRUNC(number,num_digits)
Number 需要截尾取整的数字。
Num_digits 用于指定取整精度的数字。Num_digits 的默认值为 0。
TRUNC()函数截取时不进行四舍五入
*/
9.select trunc(123.458) from dual --123
10.select trunc(123.458,0) from dual --123
11.select trunc(123.458,1) from dual --123.4
12.select trunc(123.458,-1) from dual --120
13.select trunc(123.458,-4) from dual --0
14.select trunc(123.458,4) from dual --123.458
15.select trunc(123) from dual --123
16.select trunc(123,1) from dual --123
17.select trunc(123,-1) from dual --120

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' 会提示格式不对。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • select to_date('2005-12-25,13:25:59','yyyy-mm-dd,hh24:mi:...
    snailever阅读 5,072评论 0 1
  • ORACLE日期时间函数大全 TO_DATE格式(以时间:2007-11-02 13:45:25为例) Year:...
    雨一流阅读 3,902评论 0 2
  • 转载blog.csdn.net/haiross/article/details/12837033 TRUNC函数用...
    forever_smile阅读 10,259评论 0 1
  • 1.简介 数据存储有哪些方式?电子表格,纸质文件,数据库。 那么究竟什么是关系型数据库? 目前对数据库的分类主要是...
    乔震阅读 5,867评论 0 2
  • 活动营销包括:制造新闻、改变态度、达成销售。 一、制造新闻 在项目亮相阶段,通过制造新闻的形式对项目进行宣传,扩大...