添加日期不超过一个月 [英] Add date without exceeding a month

查看:156
本文介绍了添加日期不超过一个月的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望有人可以帮助我。



我想在数据库日期添加一个月,但是我想在这些日子里阻止两个月的跳跃



例如我可能有:



2009年1月31日



我想获得



2009年2月28日



而不是

2009年3月2日



下一个日期将是



2009年3月28日



2009年6月28日



等。



有没有在oracle中执行这种操作的功能?



strong>



Yeap。我想将每个月的所有记录复制到下一个状态(所以用户不必每个月再输入2,000行)



我可以全部获取记录并手动更新日期(以一种必要的方式),但我宁愿让SQL做这个工作。



这样的东西:

  insert into the_table 
选择f1,f2,f3,f_date + 30 / *排序...:S * / from the_Table其中date> ; ?

但最后一天出现问题。



 对于
中的每个记录createObject(record)
object.date + date blabala
if(date> 29,如果februrary和月亮和星星等9

end

更新.... et

编辑:2



添加月份伎俩。



现在我只是这样:

  insert into my_table 
从my_table中选择f1,add_months(f2,1)where status = etc etc

感谢您的帮助。

解决方案

Oracle有一个内置函数<这样做的一个href =http://download.oracle.com/docs/cd/B10501_01/server.920/a96540/functions5a.htm =nofollow noreferrer> ADD_MONTHS :

  SQL> select add_months(date'2 008-01-31',1)从双重; 

ADD_MONTHS(
-----------
29-FEB-2008

SQL> select add_months(date'2008 -02-29',1)from dual;

ADD_MONTHS(
-----------
31-MAR-2008


I hope someone could help me on this.

I want to add a month to a database date, but I want to prevent two jumping over month on those days at the end.

For instance I may have:

Jan 31 2009

And I want to get

Feb 28 2009

and not

March 2 2009

Next date would be

March 28 2009

Jun 28 2009

etc.

Is there a function that already perform this kind of operation in oracle?

EDIT

Yeap. I want to copy each month all the records with some status to the next ( so the user don't have to enter again 2,000 rows each month )

I can fetch all the records and update the date manually ( well in an imperative way ) but I would rather let the SQL do the job.

Something like:

insert into the_table 
select f1,f2,f3, f_date + 30 /* sort of ... :S */  from the_Table where date > ? 

But the problem comes with the last day.

Any idea before I have to code something like this?

for each record in 
   createObject( record )
   object.date + date blabala
   if( date > 29 and if februrary and the moon and the stars etc etc 9 

end 

update....  et

EDIT:2

Add months did the trick.

now I just have this:

insert into my_table
select f1, add_months( f2, 1 ) from my_table where status = etc etc 

Thanks for the help.

解决方案

Oracle has a built-in function ADD_MONTHS that does exactly that:

SQL> select add_months(date '2008-01-31',1) from dual;

ADD_MONTHS(
-----------
29-FEB-2008

SQL> select add_months(date '2008-02-29',1) from dual;

ADD_MONTHS(
-----------
31-MAR-2008

这篇关于添加日期不超过一个月的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆