单行函数之日期函数

单行函数 : 从字面上就不难看出,单行函数就是针对单行,单个结果,单个返会的函数
也就是说 单行函数的性质是 一对一的(一条数据的战争)。

日期函数:属于单行函数的一种,主要是针对日期的转换 和 加减运算

日期的格式转换
这个是转换前的日期格式
在这里插入图片描述o

这个是转换后的 利用to_char 函数 把日期转换为字符串格式(和利用自属性封装 日期格式转换类似)
在这里插入图片描述

---------------------格式化日期
-----------------------------------TO_CHAR(SYSDATE(),'YY/MM/DD HH24:MI:SS)
-----------------------------------TO_DATE(SYSDATE(),'YY/MM/DD HH24:MI:SS)
select to_char(sysdate,‘yyyy-mm-dd hh12:mi:ss’) from dual

select to_char(sysdate,‘yyyy-mm-dd hh24:mi:ss’) from dual ---------针对字符串的转换

select to_char(sysdate,‘yyyy-mm-dd hh24:mm:ss’) from dual ----只更新秒? 很迷

select to_char(sysdate,‘yyyy-mm-dd hh24:mi:ss’) from dual

select to_date(‘2018-07-25’,‘yyyy-mm-dd hh12:mi:ss’)from dual --------------to_date 用法 只显示日期 针对数值的转换

select to_date(sysdate(),‘yyyy-mm-dd hh12:mi:ss’)from dual ----------报错原因 to_date 用法 针对数值的转换

select to_date(‘2018-07-25 15:54:34’,‘yyyy-mm-dd,hh24:mi:ss’) from dual------报错原因,todate 的时间格式是24进制

下面是日期转换的各个元素

select to_char(hire_date,‘year-mm-dd’) from employees

select to_char(hire_date,‘year’) from employees --年

select to_char(hire_date,‘MM’) from employees --月

select to_char(hire_date,‘mon’) from employees --月

select to_char(hire_date,‘dy’) from employees --星期

select to_char(hire_date,‘day’) from employees --星期

select to_char(hire_date,‘dd’) from employees --日

Note: 各个元素可以相互组合

-----------两日期相差的月数
select months_between(‘01-7月-99’,‘01-7月-98’) from dual
-----------两日期相差的天数
select floor(sysdate - to_date(‘20190401’,‘yyyymmdd’)) from dual;
-----------本月的最后一天
select last_day (‘01-7月-99’) from dual;
----今年的天数
select add_months(trunc(sysdate,‘year’), 12) - trunc(sysdate,‘year’) from dual
-----------今天星期几
select to_char(to_date(‘1998-08-15’,‘yy-mm-dd’),‘day’) from dual;
-----------当前日期的下个星期的第一天,可以看出,1是第一天 星期天
select next_day(sysdate,1)from dual;
-----------今天是今年的第几天
select to_char(sysdate,‘DDD’),sysdate from dual

猜你喜欢

转载自blog.csdn.net/weixin_44547407/article/details/89082085