oracle获取上一旬的开始时间和结束时间的实现函
网络编程 2021-07-05 16:08www.168986.cn编程入门
本文为大家介绍下oracle如何获取上一旬的开始时间和结束时间,实现函数如下,感兴趣的朋友可以参考下
代码如下:
-- 获取上旬开始时间
create or replace function fd_lastxunstart(rq in date) return string is
refstr varchar2(50);
v_rq date;
begin
--获取上一旬的日期
v_rq := trunc(rq);
select case decode(trunc((to_char(v_rq, 'dd') - 1) / 10),
0,
'上旬',
1,
'中旬',
'下旬')
when '上旬' then --返回上个月的下旬
to_char(add_months(v_rq, -1), 'yyyyMM') || '21'
when '中旬' then
to_char(v_rq, 'yyyymm') || '01' else
to_char(v_rq, 'yyyymm') || '11'
end
into refstr
from dual;
return refstr;
end fd_lastxunstart;
-- 这个返回的是上旬的开始日期
select sysdate from dual;
select fd_lastxunstart(sysdate) from dual;
select fd_lastxunstart(to_date('20130305','yyyymmdd')) from dual;
select fd_lastxunstart(to_date('20130311','yyyymmdd')) from dual;
select fd_lastxunstart(to_date('20130325','yyyymmdd')) from dual;
-- 执行结果为 2013/9/5 12:08:39、20130821、20130221、20130301、20130311
---- 获取上一旬的结束日期
-- 传递进去 一个 date 类型的值,返回一个varchar类型的上旬结束日期
create or replace function fd_lastxunend(rq in date) return string is
refstr varchar2(50);
v_rq date;
begin
--获取上一旬的日期
v_rq := trunc(rq);
select case decode(trunc((to_char(v_rq, 'dd') - 1) / 10),
0,
'上旬',
1,
'中旬',
'下旬')
when '上旬' then --返回上个月的1天
--chr(39) 这个是加引号
to_char(last_day(add_months(v_rq, -1)) + 1 - 1 / 24 / 60 / 60,
'yyyymmdd')
when '中旬' then
to_char(v_rq, 'yyyymm') || '10' else
to_char(v_rq, 'yyyymm') || '20'
end
into refstr
from dual;
return refstr;
end fd_lastxunend;
-- 这个获取的是上旬的结束日期
select fd_lastxunend(sysdate) from dual;
select fd_lastxunend(to_date('20130305','yyyymmdd')) from dual;
select fd_lastxunend(to_date('20130311','yyyymmdd')) from dual;
select fd_lastxunend(to_date('20130315','yyyymmdd')) from dual;
select fd_lastxunend(to_date('20130221','yyyymmdd')) from dual;
--执行结果20130831、20130228、20130310、20130310、20130220
-- 观察 1 / 24 / 60 / 60 的作用 这个是一秒
select last_day(add_months(trunc(sysdate), -1)) + 1 - 1 / 24 / 60 / 60
from dual;
select last_day(add_months(trunc(sysdate), -1)) from dual;
select last_day(add_months(trunc(sysdate), -1)) + 1 from dual;
-- 执行结果2013/8/31 23:59:59、2013/8/31、2013/9/1
编程语言
- 宿迁百度关键词排名指南:实现精准营销的关键
- 四川SEO优化怎么做网络推广
- 立昂技术备案老域名收购:如何为您的业务赋能
- 安徽百度关键词seo贵不贵,一般需要多少钱
- 吉林百度快照排名怎么做电话营销
- 多伦新手做SEO怎么做
- 甘肃优化关键词排名推广怎么做论坛营销
- 沙雅SEO网站推广:提升您的在线可见性
- 四川SEO优化如何提升销售额和销售量
- 聂荣网站排名优化:提升网站可见性的全方位指
- 涞水SEO:提升地方企业在线可见性的策略
- 辽宁百度seo排名怎样做网站排名
- 临湘哪有关键词排名优化:提升网站可见度的关
- 黑龙江百度网站优化有没有优惠
- 凉城优化关键词排名推广:提升您的网络可见性
- 萝北整站优化:提升您网站流量和排名的全面指