简体   繁体   English

如何计算1个月currentDate之后的日期

[英]How to calculate a date after 1 month currentDate

How can I calculate a date after a month of current date. 如何计算当前日期一个月后的日期。

Example if I have 19/04/2012 I want to get 19/05/2012. 例如,如果我有19/04/2012我想要获得19/05/2012。 Is there any function on sql that allows doing this? 在sql上有任何允许这样做的功能吗?

I need to do the same for a year also like 19/04/2012 and set 19/04/2013 我需要做同样的一年,如19/04/2012和19/04/2013

Cheers 干杯

You can use DATEADD (datepart , number , date ) 你可以使用DATEADD(datepart,number,date)

as specified here 如此处所述

Examples (thanks to DarrenDavis) 示例(感谢DarrenDavis)

for month: 月份:

 select dateadd(m, 1, getdate())

for year: 年份:

 select dateadd(YY, 1, getdate())

使用dateadd()函数

SELECT dateadd(mm, 1, '19-Apr-2012')

To add 1 month to the current date: 要在当前日期添加1个月:

SELECT DATEADD(m, 1, GETDATE())

To add 1 year to the current date: 要在当前日期添加1年:

SELECT DATEADD(YY, 1, GETDATE())

For additional arguments see: 有关其他参数,请参阅

http://msdn.microsoft.com/en-us/library/ms186819.aspx http://msdn.microsoft.com/en-us/library/ms186819.aspx

You can use DateAdd: 您可以使用DateAdd:

select dateadd(m, 1, getdate())
select dateadd(yy, 1, getdate())

search online for other period indicators such as hour, minute, etc! 在线搜索其他时段指标,如小时,分钟等!

Something like this should do the job 像这样的东西应该做的工作

SELECT DATEADD(month, 1, '2012-04-19') SELECT DATEADD(月,1,'2012-04-19')

for a year 一年

SELECT DATEADD(year, 1, '2012-04-19') SELECT DATEADD(年,1,'2012-04-19')

http://msdn.microsoft.com/en-us/library/ms186819.aspx http://msdn.microsoft.com/en-us/library/ms186819.aspx

You can use this query for 1 month ahead date . 您可以提前1个月使用此查询。 This query works fine in oracle. 此查询在oracle中正常工作。

select add_months(trunc(sysdate),1) from dual;

For 1 year ahead date use this query 提前1年使用此查询

select add_months(trunc(sysdate),12) from dual;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM