oracle trunc 函数处理日期格式,日期类型很有用的几个sql
来源:互联网 发布:淘宝店保证金能退吗 编辑:程序博客网 时间:2024/05/21 10:32
select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') from dual; --显示当前时间
2011-12-29 16:24:34
select trunc(sysdate,'year') from dual; --截取到年(本年的第一天)
2011-1-1
select trunc(sysdate,'q') from dual; --截取到季度(本季度的第一天)
2011-10-1
select trunc(sysdate,'month') from dual; --截取到月(本月的第一天)
2011-12-1
select trunc(sysdate,'') from dual;
空
select to_char(trunc(sysdate),'yyyymmdd hh24:mi:ss') from dual; --默认截取到日(当日的零点零分零秒)
20111229 00:00:00
select trunc(sysdate-1,'w') from dual; -- 离当前时间最近的周四,若当天为周四则返回当天,否则返回上周四
2011-12-22
select trunc(sysdate,'ww') from dual; --截取到上周末(上周周六)
2011-12-24
select trunc(sysdate,'day') from dual; --截取到周(本周第一天,即上周日)
2011-12-25
select trunc(sysdate,'iw') from dual; --本周第2天,即本周一
2011-12-26
select to_char(trunc(sysdate,'dd'),'yyyymmdd hh24:mi:ss') from dual;--截取到日(当日的零点零分零秒)
20111229 00:00:00
select trunc(sysdate,'hh24') from dual; --截取到小时(当前小时,零分零秒)
2011-12-29 16:00:00
select trunc(sysdate,'mi') from dual; --截取到分(当前分,零秒)
2011-12-29 16:24:00
select trunc(sysdate,'ss') from dual ;--报错,没有精确到秒的格式
[sql]
-- Oracle 取上周一到周末的sql
-- 这样取的是 在一周内第几天,是以周日为开始的
select
to_char(to_date(
'20130906'
,
'yyyymmdd'
),
'd'
)
from
dual;
--结果:6 注释:2013.09.06是周五,为本周的第六天
select
to_char(sysdate+(2-to_char(sysdate,
'd'
))-7,
'yyyymmdd'
)
from
dual;
---上周一
select
to_char(sysdate+(2-to_char(sysdate,
'd'
))-1,
'yyyymmdd'
)
from
dual;
---上周日
-- 一个更简单的写法 , 返回date类型
select
trunc(sysdate,
'iw'
) - 7
from
dual;
---上周一
select
trunc(sysdate,
'iw'
) - 1
from
dual;
--上周日
-- 这样查出来是本周一
select
trunc(sysdate,
'iw'
)
from
dual;
select
trunc(to_date(
'20130915'
,
'yyyymmdd'
),
'iw'
)
from
dual;
-- 结果:2013/9/9 注释:20130915 为周日
-- 返回char类型
select
to_char(trunc(sysdate,
'iw'
) - 7,
'yyyymmdd'
)
from
dual;
--上周一
select
to_char(trunc(sysdate,
'iw'
) - 1,
'yyyymmdd'
)
from
dual;
--上周日
-- 获取上周一的函数
create
or
replace
function
fun_acc_getlastweekstart(systemdate
in
date
)
return
varchar2
is
result_str varchar2(15);
begin
select
to_char(trunc(systemdate,
'iw'
) - 7,
'yyyymmdd'
)
into
result_str
from
dual;
return
result_str;
end
fun_acc_getlastweekstart;
-- 获取上周日的函数
create
or
replace
function
fun_acc_getlastweekend(systemdate
in
date
)
return
varchar2
is
result_str varchar2(15);
begin
select
to_char(trunc(systemdate,
'iw'
) - 1,
'yyyymmdd'
)
into
result_str
from
dual;
return
result_str;
end
fun_acc_getlastweekend;
-- 测试这个函数
select
fun_acc_getlastweekstart(sysdate)
from
dual;
select
fun_acc_getlastweekend(sysdate)
from
dual;
select
fun_acc_getlastweekstart(to_date(
'20130915'
,
'yyyymmdd'
))
from
dual;
select
fun_acc_getlastweekend(to_date(
'20130915'
,
'yyyymmdd'
))
from
dual;
--查询结果:20130826、20130901、20130902、20130908
-- 注:
select
sysdate
from
dual;
--查询结果:2013/9/6 9:45:14
- oracle trunc 函数处理日期格式,日期类型很有用的几个sql
- oracle trunc 函数处理日期格式,日期类型很有用的几个sql
- oracle trunc 函数处理日期格式,日期类型很有用的几个sql
- oracle trunc 函数处理日期格式,日期类型很有用的几个sql
- oracle trunc 函数处理日期格式,日期类型很有用的几个sql
- Oracle trunc 函数处理日期格式,日期类型
- oracle trunc 函数处理日期格式
- oracle 日期函数 TRUNC
- trunc()函数用法处理日期、数字类型数据
- oracle函数记录-trunc日期格式ww、iw、w的区别
- 有用的函数-日期/日历处理函数
- postgre实现类似Oracle的trunc日期函数
- trunc函数处理日期和数字
- Oracle数字、日期截取函数TRUNC
- Oracle 日期格式类型和日期函数 java操作数据库
- Oracle 日期格式类型和日期函数 java操作数据库
- Oracle 日期格式处理
- 有用的oracle 日期
- UML3
- eclipse解决重写方法参数是arg0,arg1等
- javascript中的preventDefault与stopPropagation作用介绍
- Eclipse代码自动补全设置
- cocos2d-x学习笔记-SpriteBatchNode的优化原理
- oracle trunc 函数处理日期格式,日期类型很有用的几个sql
- TCP/IP协议族——ARP、DNS工作原理及实例详解
- Python学习(1)基本类型
- LeetCode: Unique Paths
- Oracle 10g RAC 添加节点
- selenium使用中遇到的问题
- Fedora 20: How to enable SSH
- 设计模式(3)——观察者模式(Observer Pattern)
- DecimalFormat用法