@oracle自界说函数写excel中DATE函数
oracle自界说函数写excel中DATE函数
EXCEL中DATE函数逻辑为DATE(year,month,day)
寄义:DATE(年,月,日)
EXCEL理解逻辑如下:
- Year 参数的值可以包罗一到四位数字。Excel 将根据盘算机正在使用的日期系统来表明 year 参数。默认情况下,Microsoft Excel for Windows 使用的是 1900 日期系统,这表示第一个日期为 1900 年 1 月 1 日。
- 如果 year 介于 0(零)到 1899 之间(包罗这两个值),则 Excel 会将该值与 1900 相加来盘算年份。比方,DATE(108,1,2) 返回 2008 年 1 月 2 日 (1900+108)。
- 如果 year 介于 1900 到 9999 之间(包罗这两个值),则 Excel 将使用该数值作为年份。比方,DATE(2017,12,5) 将返回 2017 年 12月 5日。
- 如果 year 小于 0 或大于即是 10000,则 Excel 返回 错误值 #NUM!。
- Month 一个正整数或负整数,表示一年中从 1 月至 12 月(一月到十二月)的各个月。
- 如果 month 大于 12,则 month 会从指定年份的第一个月开始加上该月份数。比方,DATE(2016,14,2) 返回表示 2017 年 2 月 2 日的序列数。
- 如果 month 小于 1,则 month 会从指定年份的第一个月开始减去该月份数,然后再加上 1 个月。比方,DATE(207,-3,2) 返回表示 2016 年 9 月 2 日的序列号。
- Day 一个正整数或负整数,表示一月中从 1 日到 31 日的各天。
- 如果 day 大于指定月中的天数,则 day 会从该月的第一天开始加上该天数。比方,DATE(2017,1,35) 返回表示 2017 年 2 月 4 日的序列数。
- 如果 day 小于 1,则 day 从指定月份的第一天开始减去该天数,然后再加上 1 天。比方,DATE(2017,1,-15) 返回表示 2016 年 12 月 16 日的序列号。
ORACLE自界说函数代码如下:
- --创建函数create or replace function term_date(year_p number, month_p number, day_p number) return date is term date; --excel中的date(year,month,day)的逻辑,转化成函数begin --判定年份 --如果年份在0-1899之间,则使用1900与年份相加 if year_p >= '0' and year_p 12 or month_p < 1 then --如果天数大于指定月份的天数,则从该月1号开始,加天数 --如果天数小于1,则从该月1号开始,减天数 if day_p > to_char(last_day(ADD_MONTHS(to_date(to_char(1900 + year_p) || '-' || to_char(1), 'yyyy-mm'), month_p - 1)), 'dd') or day_p < 1 then select ADD_MONTHS(to_date(to_char(1900 + year_p) || '-' || to_char(1) || '-' || to_char(1), 'yyyy-mm-dd'), month_p - 1) + day_p - 1 into term from dual; --天数正常 else select ADD_MONTHS(to_date(to_char(1900 + year_p) || '-' || to_char(1) || '-' || day_p, 'yyyy-mm-dd'), month_p - 1) into term from dual; end if; --月份正常情况 else --如果天数大于指定月份的天数,则从该月1号开始,加天数 --如果天数小于1,则从该月1号开始,减天数 if day_p > to_char(last_day(to_date(to_char(1900 + year_p) || '-' || to_char(month_p), 'yyyy-mm')), 'dd') or day_p < 1 then select to_date(to_char(1900 + year_p) || '-' || to_char(month_p) || '-' || to_char(1), 'yyyy-mm-dd') + day_p - 1 into term from dual; --天数正常 else select to_date(to_char(1900 + year_p) || '-' || to_char(month_p) || '-' || to_char(day_p), 'yyyy-mm-dd') into term from dual; end if; end if; --如果年份在1900-9999之间,则判定月份 elsif year_p >= '1900' and year_p 12 or month_p < 1 then --如果天数大于指定月份的天数,则从该月1号开始,加天数 --如果天数小于1,则从该月1号开始,减天数 if day_p > to_char(last_day(ADD_MONTHS(to_date(to_char(year_p) || '-' || to_char(1), 'yyyy-mm'), month_p - 1)), 'dd') or day_p < 1 then select ADD_MONTHS(to_date(to_char(year_p) || '-' || to_char(1) || '-' || to_char(1), 'yyyy-mm-dd'), month_p - 1) + day_p - 1 into term from dual; --天数正常 else select ADD_MONTHS(to_date(to_char(year_p) || '-' || to_char(1) || '-' || day_p, 'yyyy-mm-dd'), month_p - 1) into term from dual; end if; --月份正常情况 else --如果天数大于指定月份,则从该月1号开始,加天数 --如果天数小于1,则从该月1号开始,减天数 if day_p > to_char(last_day(to_date(to_char(year_p) || '-' || to_char(month_p), 'yyyy-mm')), 'dd') or day_p < 1 then select to_date(to_char(year_p) || '-' || to_char(month_p) || '-' || to_char(1), 'yyyy-mm-dd') + day_p - 1 into term from dual; --天数正常 else select to_date(to_char(year_p) || '-' || to_char(month_p) || '-' || to_char(day_p), 'yyyy-mm-dd') into term from dual; end if; end if; end if; return term;end term_date;
复制代码 希望对各人有所资助,如有问题,请多多指出
来源:https://blog.csdn.net/qq_43006059/article/details/112008415
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |