简体   繁体   中英

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. Is there any function on sql that allows doing this?

I need to do the same for a year also like 19/04/2012 and set 19/04/2013

Cheers

You can use DATEADD (datepart , number , date )

as specified here

Examples (thanks to 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:

SELECT DATEADD(m, 1, GETDATE())

To add 1 year to the current date:

SELECT DATEADD(YY, 1, GETDATE())

For additional arguments see:

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

You can use 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')

for a year

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

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

You can use this query for 1 month ahead date . This query works fine in oracle.

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

For 1 year ahead date use this query

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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