oracle 数据库日期定义,Oracle数据库实现日期遍历功能

遍历开始日期到结束日期的每一天,若有查询某段日期下有什么业务或者事件发生时,可用到此函数。 Oracle SQL Developer create or replace type class_date as object( year varchar2(10), month varchar2(10), day varchar2(20))--定义所需要的日期类-------

遍历开始日期到结束日期的每一天,若有查询某段日期下有什么业务或者事件发生时,可用到此函数。

Oracle SQL Developer

create or replace type class_date as object

(

year varchar2(10),

month varchar2(10),

day varchar2(20)

)--定义所需要的日期类

-----------------------------------------------------------------------------

create or replace type table_date is table of class_date--日期类返回table类型

------------------------------------------------------------------------------

create or replace function minusDay(firstDay in varchar2,lastDay in varchar2)

return table_date pipelined

as

firstYear number;

firstMonth number;

lastYear number;

lastMonth number;

totalDay number;

totalMonth number;

currentDay varchar2(40);

currentYear varchar2(40);

type tt is record(

day varchar2(20),

month varchar2(20),

year varchar2(20)

);

v_date tt;

begin

--第一天的日期转换

select to_number(substr(firstDay,1,4))into firstYear from dual ;

select to_number(substr(firstDay,6,2)) into firstMonth from dual;

--第二天的日期转换

select to_number(substr(lastDay,1,4)) into lastYear from dual;

select to_number(substr(lastDay,6,2)) into lastMonth from dual;

--1 第一个日期早于第二个日期

if to_number(to_date(firstDay,'yyyy-mm-dd')-to_date(lastDay,'yyyy-mm-dd')) <0 then

dbms_output.put_line('第一个日期早于第二个日期!');

end if;

--2 第一个日期晚于第二个日期

--------相同年份

if firstYear = lastYear then

v_date.year := to_char(firstYear);

--------相同月份

if firstMonth = lastMonth then

v_date.month := to_char(firstMonth);

-- 天数差

totalDay := to_number(to_date(lastDay,'yyyy-mm-dd')-to_date(firstDay,'yyyy-mm-dd'));

if totalDay = 0 then

v_date.day := firstDay;

pipe row(class_date(v_date.year,v_date.month,v_date.day));

elsif totalDay >0 then

for dayId in to_number(substr(firstDay,9,2))..to_number(substr(lastDay,9,2)) loop

v_date.day :=to_char(substr(firstDay,1,7)||'-'||to_char(dayId)) ;

pipe row(class_date(v_date.year,v_date.month,v_date.day));

dbms_output.put_line( v_date.day);

end loop;

end if;

--------不同月份

elsif firstMonth < lastMonth then

---月份差

--totalMonth := lastMonth - firstMonth;

for id in firstMonth..lastMonth-1 loop

v_date.month := to_char(id);

--满月天数差

totalDay := to_number(last_day(to_date(firstYear||'-'||to_char(id)||'-'||'01','yyyy-mm-dd'))-to_date(firstYear||'-'||to_char(id)||'-'||'01','yyyy-mm-dd'))+1;

for dayId in 1..totalDay loop

v_date.day := substr(firstDay,1,7)||'-'||to_char(dayId) ;

pipe row(class_date(v_date.year,v_date.month,v_date.day));

dbms_output.put_line( v_date.day);

end loop;

end loop;

--最后一个月的遍历

v_date.month := to_char(lastMonth);

-- totalDay :=to_date(lastMonth,'yyyy-mm-dd') -to_date(to_char(lastYear)||'-'||to_char(lastMonth)||'-01','yyyy-mm-dd');

totalDay := to_number(to_date(lastDay,'yyyy-mm-dd')-to_date(substr(lastDay,1,7)||'-01','yyyy-mm-dd'))+1;

for id in 1..totalDay loop

v_date.day := substr(lastDay,1,7)||'-'||to_char(id);

pipe row(class_date(v_date.year,v_date.month,v_date.day));

dbms_output.put_line( v_date.day);

end loop;

end if;

--------不同年份

elsif firstYear < lastYear then

----------------------------------------------

--第一个月

v_date.year := to_char(firstYear);

v_date.month := substr(firstDay,6,2);

totalDay :=to_number(substr( to_char(last_day(to_date(firstDay,'yyyy-mm-dd')),'yyyy-mm-dd'),9,2));

for dayId in to_number(substr(firstDay,9,2)) ..totalDay loop

v_date.day := to_char(dayId);

pipe row(class_date(v_date.year,v_date.month,v_date.day));

end loop;

---------------------------------------------------------------

--中间所有月数的日期添加

totalMonth := to_number( months_between(to_date(lastDay,'yyyy-mm-dd'),to_date(firstDay,'yyyy-mm-dd')))-1;

currentDay := firstDay;

currentDay := substr(currentDay,1,8)||'01';

for monthId in 1..totalMonth loop

--月数循环

currentDay:=

to_char(add_months(to_date(currentDay,'yyyy-mm-dd'),1),'yyyy-mm-dd') ;

currentYear := substr(currentDay,1,4);

v_date.year := to_char(substr(trim(currentDay),1,4));

v_date.month := to_char(substr(trim(currentDay),6,2));

--天数循环

totalDay := to_number(last_day(to_date(currentDay,'yyyy-mm-dd'))-to_date(currentYear||'-'||substr(currentDay,6,2)||'-01','yyyy-mm-dd'))+1;

for dayId in 1 .. totalDay loop

v_date.day := to_char(dayId);

pipe row(class_date(v_date.year,v_date.month,v_date.day));

dbms_output.put_line( v_date.day);

end loop;

end loop;

-----------------------------------------------------------------------------------

--最后一个月

totalDay := to_number(substr(lastDay,9,2));

v_date.month := to_number(substr(lastDay,6,2));

for dayId in 1 .. totalDay loop

v_date.day := to_char(dayId);

pipe row(class_date(v_date.year,v_date.month,v_date.day));

end loop;

end if;

end minusDay;

本文原创发布php中文网,转载请注明出处,感谢您的尊重!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/554908.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

linux tomcat php配置文件在哪个文件夹下,tomcat下,怎么安配备置php ?(linux系统)

(一)、JDK安装tar.gz为解压后就可使用的版本&#xff0c;这里我们将jdk-8u45-linux-i586.tar.gz解压到/usr/local/下。1、解压[rootTomcat~]#tar-zxvfjdk-8u45-linux-i586.tar.gz2、环境配置[rootTomcat~]#sudovi/etc/profile#setjavaenvironmentJAVA_HOME/usr/local/jdk1.8.0C…

Java递归生成树

1.建菜单表 CREATE TABLE t_menu (id int(11) NOT NULL AUTO_INCREMENT,pid int(11) NOT NULL,name varchar(255) DEFAULT NULL,PRIMARY KEY (id) ) ENGINEInnoDB AUTO_INCREMENT11 DEFAULT CHARSETutf8mb4;2.造一些数据 注意&#xff1a;根节点的pid0&#xff0c;其他节点的p…

linux内核时钟驱动,4.9版本的linux内核中实时时钟芯片pcf85263的驱动源码在哪里

SQL添加维护 计划失败在sql要求数据库每天自动备份这个是大家都会遇到的问题,我遇到了这个问题如图: 是因为这个服务组件没有安装CSS Hack汇总快查&lpar;CSS兼容代码演示&rpar;文章出处和来源网址:http://www.divcss5.com/css-hack/c284.shtml 以下是常用CSS HACK问题及…

Springboot获取公网IP和当前所在城市(非常简单)

最近我们发现各大社交平台都出现了一个新的功能&#xff1a;IP属地。 比如某乎&#xff1a; 这个IP属地是怎么做到的呢&#xff1f;今天我来教教你&#xff0c;保证你看完直呼Easy~ 百度搜索 打开百度&#xff0c;搜索IP&#xff0c;你就能看到你当前的IP地址&#xff0c;类…

linux启动脚本卡住,linux 服务脚本启动问题

对于使用了 systemd 的系统&#xff0c;所有的 service 服务都会默认转为 systemd 服务之后再由 systemd 来执行&#xff0c;转换之后&#xff0c;你也可以直接使用 systemd 来执行了(它的用户工具就是你用的 systemctl)&#xff0c;除非是一些非 service 标准的命令&#xff0…

Springboot集成百度地图实现定位打卡功能

打卡sign表sql CREATE TABLE sign (id int(11) NOT NULL AUTO_INCREMENT,user varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT 用户名称,location varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT 打卡位置,time varchar(255) COLLATE utf8mb4…

linux版本i686,linux-x86_64平台上的gcc i686

我在RHEL X86_64上安装GCC i686时遇到一些麻烦.确实,我必须在此平台上构建一些32位软件和共享库.我可以在32位平台(Linux或Windows)上构建这些软件和库.我的问题在这篇文章的结尾.我的第一个问题是这个错误&#xff1a;(在buil期间,在Eclipse -helios下)In file included from …

线程死锁——死锁产生的条件

什么是线程死锁 线程死锁是指由于两个或者多个线程互相持有对方所需要的资源&#xff0c;导致这些线程处于相互等待状态&#xff0c;若无外力作用&#xff0c;它们将无法继续执行下去。 造成死锁的原因可以概括成三句话&#xff1a; 当前线程拥有其他线程需要的资源当前线程…

linux查看正在运行的窗口,获取linux中打开的应用程序窗口的数量

我想检测由窗口管理器管理的特定应用程序的实例数量.目前,我有这个&#xff1a;#!/bin/bash# wmctrl requiredpids$(pidof $1)IFS read -a pid_arr <<< "$pids"matches0for pid in "${pid_arr[]}"domatching_lines$(wmctrl -l -p | egrep -c &qu…

TortoiseGit的使用详解

Git是什么&#xff0c;相信大家都很清楚。Git不就是分布式版本控制系统嘛&#xff1f;那你知道TortoiseGit是什么吗&#xff1f;下面我们就介绍一下TortoiseGit它是什么&#xff1f;如何使用&#xff1f;   TortoiseGit其实是一款开源的git的版本控制系统&#xff0c;也叫海龟…

linux gpt分区看不到,Linux无法看到我的任何分区 – 备份GPT表不在磁盘的末尾

我正在尝试在HP Pavilion 14英寸超极本上安装Linux,但没有任何成功.起初我尝试在其上安装Ubuntu;一切都很顺利,我进入了Live DVD(是的,我就像那样老了),然后去我的磁盘上安装系统.发生的第一个奇怪的事情是,我没有被提示选择在Windows旁边安装Ubuntu,而是直接用分区表抛入窗口.…

将项目上传到Gitee上(命令方式使用TortoiseGit方式)

如何将项目上传到Gitee上&#xff08;命令方式&#xff09; 目录 将项目上传到Gitee是我们经常需要使用到的操作&#xff0c;因此我们要熟悉这些步骤 一、首先保证本机已经安装了Git git官网安装完成之后&#xff0c;鼠标右键会出现Git GUI Here和Git Bash Here 二、上传代…

linux自动重新启动,linux 系统自动重新启动,请帮忙看看

在查了一下,的确有这个log其中有一段之后系统开始重新启动&#xff0c;请帮忙看看是什么原因&#xff1a;谢谢[2011-01-25 11:33:36 xend.XendDomainInfo 2990] DEBUG (XendDomainInfo:228) XendDomainInfo.recreate({paused: 0, cpu_time: 41195236230L, ssidref: 0, hvm: 0, …

java.awt.Color类

Color类概述 Color是用来封装颜色的&#xff0c;支持多种颜色空间&#xff0c;默认为RGB颜色空间。每个Color对象都有一个alpha通道&#xff0c;值为0到255&#xff0c;代表透明度&#xff0c;当alpha通道值为255时&#xff0c;表示完全不透明&#xff1b;当alpha通道值为0时&…

BufferedImage类、Image类、Graphics类

BufferedImage Image是一个抽象类&#xff0c;BufferedImage是其实现类&#xff0c;是一个带缓冲区图像类&#xff0c;主要作用是将一幅图片加载到内存中&#xff08;BufferedImage生成的图片在内存里有一个图像缓冲区&#xff0c;利用这个缓冲区我们可以很方便地操作这个图片&…

linux远程连接最大数是多少,Linux Shell 脚本限制ssh最大用户登录数

我撰写本文原来的意图是想把“复制SSH渠道”和"copy SSH Session"这样的功能从远程ssh客户端中剔除掉.因此想到可以在SSH服务端设置一下&#xff0c;但查阅了sshd_config的man手册,发现里面的看起来限制ssh连接数量的参数(MaxSessions &#xff0c;ClientAliveCountM…

linux 文件名带特殊符号,Linux删除含有特殊符号文件名的文件

Web前端面试题目及答案汇总HTML/CSS部分 1.什么是盒子模型? 在网页中,一个元素占有空间的大小由几个部分构成,其中包括元素的内容(content),元素的内边距(padding),元素的边框(border),元素的外边 ...Delphi中滚动文字的应用1.添加一个Timer控件,Interval属性设置为20. 2.添加…

Vue this.$refs的作用

案例一、ref 写在标签上时 <!-- ref 写在标签上时&#xff1a;this.$refs.名字 获取的是标签对应的dom元素ref 写在组件上时&#xff1a;这时候获取到的是 子组件&#xff08;比如counter&#xff09;的引用--><div id"root"><!-- ref hello&#…

linux电脑合盖后卡住了,解决ubuntu合盖后无法唤醒

解决办法&#xff1a;安装laptop-mode-tools工具包。1.检查是否安装了grep laptop-mode-tools 工具包$ dpkg -l | grep laptop-mode-tools如果执行命令无结果输出&#xff0c;表示未安装(如果已安装&#xff0c;忽略第2步)2.安装laptop-mode执行命令&#xff1a;$ sudo apt-get…

三列布局 css

实现如下图的三列布局&#xff1a; .box {width:1400px;margin:0 auto;padding-bottom:40px;> .left {float:left;width:180px;margin-top:100px;text-align:center;}> .center {float:left;margin-top:100px;margin-left:130px;item-box {float:left;text-align:left;…